OSDN Git Service

d3a461a690c626bbdc055f910ce58ca269124978
[gokigen/Gr2Control.git] / app / src / main / java / net / osdn / gokigen / gr2control / camera / ricohgr2 / wrapper / RicohGr2StatusHolder.java
1 package net.osdn.gokigen.gr2control.camera.ricohgr2.wrapper;
2
3 import android.support.annotation.NonNull;
4 import android.util.Log;
5
6 import net.osdn.gokigen.gr2control.liveview.ICameraStatusUpdateNotify;
7
8 import org.json.JSONArray;
9 import org.json.JSONObject;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15  *
16  *
17  */
18 class RicohGr2StatusHolder
19 {
20     private final String TAG = toString();
21     private final ICameraStatusUpdateNotify notifier;
22
23     private JSONObject latestResultObject = null;
24     private boolean focused = false;
25     private boolean focusLocked = false;
26     private String avStatus = "";
27     private String tvStatus = "";
28     private String xvStatus = "";
29     private String exposureModeStatus = "";
30     private String meteringModeStatus = "";
31     private String wbModeStatus = "";
32     private String batteryStatus = "";
33
34     /**
35      *
36      *
37      */
38     RicohGr2StatusHolder(ICameraStatusUpdateNotify notifier)
39     {
40         this.notifier = notifier;
41     }
42
43     /**
44      *
45      *
46      */
47     List<String> getAvailableItemList(@NonNull String key)
48     {
49         List<String> itemList = new ArrayList<>();
50         try
51         {
52             JSONArray array = latestResultObject.getJSONArray(key);
53             if (array == null)
54             {
55                 return (itemList);
56             }
57             int nofItems = array.length();
58             for (int index = 0; index < nofItems; index++)
59             {
60                 try
61                 {
62                     itemList.add(array.getString(index));
63                 }
64                 catch (Exception e)
65                 {
66                     e.printStackTrace();
67                 }
68             }
69         }
70         catch (Exception e)
71         {
72             e.printStackTrace();
73         }
74         return (itemList);
75     }
76
77     String getItemStatus(@NonNull String key)
78     {
79         try
80         {
81             return (latestResultObject.getString(key));
82         }
83         catch (Exception e)
84         {
85             e.printStackTrace();
86         }
87         return ("");
88     }
89
90     private String getStatusString(JSONObject obj, String name)
91     {
92         try
93         {
94             return (obj.getString(name));
95         }
96         catch (Exception e)
97         {
98             e.printStackTrace();
99         }
100         return ("");
101     }
102
103     private boolean getBooleanStatus(JSONObject obj, String name)
104     {
105         try {
106             return (obj.getBoolean(name));
107         } catch (Exception e) {
108             //e.printStackTrace();
109         }
110         return (false);
111     }
112
113     /**
114      *
115      *
116      */
117     void updateStatus(String replyString)
118     {
119         if ((replyString == null)||(replyString.length() < 1))
120         {
121             Log.v(TAG, "httpGet() reply is null. ");
122             return;
123         }
124
125         try
126         {
127             latestResultObject = new JSONObject(replyString);
128             String result = getStatusString(latestResultObject,"errMsg");
129             String av = getStatusString(latestResultObject,"av");
130             String tv = getStatusString(latestResultObject,"tv");
131             String xv = getStatusString(latestResultObject,"xv");
132             String exposureMode = getStatusString(latestResultObject,"exposureMode");
133             String meteringMode = getStatusString(latestResultObject,"meteringMode");
134             String wbMode = getStatusString(latestResultObject,"WBMode");
135             String battery = getStatusString(latestResultObject,"battery");
136             boolean focus = getBooleanStatus(latestResultObject,"focused");
137             boolean focusLock = getBooleanStatus(latestResultObject,"focusLocked");
138
139             if (result.contains("OK"))
140             {
141                 if (!avStatus.equals(av))
142                 {
143                     avStatus = av;
144                     notifier.updatedAperture(avStatus);
145                 }
146                 if (!tvStatus.equals(tv))
147                 {
148                     tvStatus = tv;
149                     notifier.updatedShutterSpeed(tvStatus);
150                 }
151                 if (!xvStatus.equals(xv))
152                 {
153                     xvStatus = xv;
154                     notifier.updatedExposureCompensation(xvStatus);
155                 }
156                 if (!exposureModeStatus.equals(exposureMode))
157                 {
158                     exposureModeStatus = exposureMode;
159                     notifier.updatedTakeMode(exposureModeStatus);
160                 }
161                 if (!meteringModeStatus.equals(meteringMode))
162                 {
163                     meteringModeStatus = meteringMode;
164                     notifier.updatedMeteringMode(meteringModeStatus);
165                 }
166                 if (!wbModeStatus.equals(wbMode))
167                 {
168                     wbModeStatus = wbMode;
169                     notifier.updatedWBMode(wbModeStatus);
170                 }
171                 if (!batteryStatus.equals(battery))
172                 {
173                     batteryStatus = battery;
174                     notifier.updateRemainBattery(Integer.parseInt(batteryStatus));
175                 }
176                 if ((focus != focused)||(focusLock != focusLocked))
177                 {
178                     focused = focus;
179                     focusLocked = focusLock;
180                     notifier.updateFocusedStatus(focused, focusLocked);
181                 }
182             }
183             System.gc();
184         }
185         catch (Exception e)
186         {
187             e.printStackTrace();
188         }
189     }
190 }