1 |
|
package com.dreamcatcher.bicycle.util; |
2 |
|
|
3 |
|
import java.io.BufferedReader; |
4 |
|
import java.io.InputStream; |
5 |
|
import java.io.InputStreamReader; |
6 |
|
|
7 |
|
import org.json.JSONArray; |
8 |
|
import org.json.JSONObject; |
9 |
|
|
10 |
|
import android.app.Activity; |
11 |
|
import android.content.Context; |
12 |
|
import android.content.Intent; |
13 |
|
import android.content.SharedPreferences; |
14 |
|
import android.content.SharedPreferences.Editor; |
15 |
|
import android.content.pm.PackageInfo; |
16 |
|
import android.content.res.AssetManager; |
17 |
|
import android.graphics.drawable.Drawable; |
18 |
|
import android.media.Ringtone; |
19 |
|
import android.media.RingtoneManager; |
20 |
|
import android.net.ConnectivityManager; |
21 |
|
import android.net.NetworkInfo; |
22 |
|
import android.net.NetworkInfo.State; |
23 |
|
import android.net.Uri; |
24 |
|
import android.os.Vibrator; |
25 |
|
import android.preference.PreferenceManager; |
26 |
|
|
27 |
|
import com.dreamcatcher.bicycle.BicycleApp; |
28 |
|
import com.dreamcatcher.bicycle.dataset.BicycleDataset; |
29 |
|
import com.dreamcatcher.bicycle.vo.BicycleStationInfo; |
30 |
|
import com.dreamcatcher.bicycle.vo.CitySetting; |
31 |
|
|
|
|
| 49% |
Uncovered Elements: 124 (243) |
Complexity: 60 |
Complexity Density: 0,35 |
|
32 |
|
public class Utils { |
33 |
|
private static SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(BicycleApp.getInstance()); |
34 |
|
private static Editor mEditor = mSharedPreferences.edit(); |
35 |
|
private static BicycleApp mBicycleApp = BicycleApp.getInstance(); |
36 |
|
private static Ringtone mRingtone = null; |
37 |
|
private static Vibrator mVibrator = null; |
38 |
|
|
|
|
| 83,3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0,33 |
|
39 |
2
|
public static PackageInfo getPackageInfo(){... |
40 |
2
|
PackageInfo packageInfo = null; |
41 |
2
|
try { |
42 |
2
|
String packageName = mBicycleApp.getPackageName(); |
43 |
2
|
packageInfo = mBicycleApp.getPackageManager().getPackageInfo(packageName, 0); |
44 |
|
} catch (Exception e) { |
45 |
0
|
e.printStackTrace(); |
46 |
|
} |
47 |
2
|
return packageInfo; |
48 |
|
} |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
50 |
22
|
public static String getStringDataFromLocal(String tagName){... |
51 |
22
|
String result = null; |
52 |
22
|
result = mSharedPreferences.getString(tagName, ""); |
53 |
22
|
return result; |
54 |
|
} |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
56 |
12
|
public static void storeStringDataToLocal(String tagName, String value){... |
57 |
12
|
mEditor.putString(tagName, value); |
58 |
12
|
mEditor.commit(); |
59 |
|
} |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
61 |
5
|
public static boolean getBooleanDataFromLocal(String tagName, boolean defaultValue){... |
62 |
5
|
boolean result = false; |
63 |
5
|
result = mSharedPreferences.getBoolean(tagName, defaultValue); |
64 |
5
|
return result; |
65 |
|
} |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
67 |
5
|
public static void storeBooleanDataToLocal(String tagName, boolean value){... |
68 |
5
|
mEditor.putBoolean(tagName, value); |
69 |
5
|
mEditor.commit(); |
70 |
|
} |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
72 |
2
|
public static int getIntDataFromLocal(String tagName, int defaultValue){... |
73 |
2
|
int result = -1; |
74 |
2
|
result = mSharedPreferences.getInt(tagName, defaultValue); |
75 |
2
|
return result; |
76 |
|
} |
77 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
78 |
0
|
public static void storeLongDataToLocal(String tagName, long value){... |
79 |
0
|
mEditor.putLong(tagName, value); |
80 |
0
|
mEditor.commit(); |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
83 |
1
|
public static long getLongDataFromLocal(String tagName, long defaultValue){... |
84 |
1
|
long result = 0; |
85 |
1
|
result = mSharedPreferences.getLong(tagName, defaultValue); |
86 |
1
|
return result; |
87 |
|
} |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
89 |
0
|
public static void storeIntDataToLocal(String tagName, int value){... |
90 |
0
|
mEditor.putInt(tagName, value); |
91 |
0
|
mEditor.commit(); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
94 |
2
|
public static void clearLocalData(){... |
95 |
2
|
mEditor.clear(); |
96 |
2
|
mEditor.commit(); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
102 |
0
|
public static void startCall(Activity activity, String phoneNum){ ... |
103 |
0
|
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum)); |
104 |
0
|
activity.startActivity(intent); |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
110 |
0
|
public static void startSMS(Activity activity, String message){... |
111 |
0
|
Intent intent = new Intent(Intent.ACTION_VIEW); |
112 |
0
|
intent.putExtra("sms_body", message); |
113 |
0
|
intent.setType("vnd.android-dir/mms-sms"); |
114 |
0
|
activity.startActivity(intent); |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
120 |
0
|
public static void startBrowser(Activity activity, String url){... |
121 |
0
|
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
122 |
0
|
activity.startActivity(intent); |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
128 |
0
|
public static void startShare(){... |
129 |
|
|
130 |
|
} |
131 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0,5 |
|
132 |
0
|
public static void reminderReturnBicycle(){... |
133 |
0
|
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); |
134 |
0
|
if(alert == null){ |
135 |
0
|
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); |
136 |
0
|
if(alert == null){ |
137 |
0
|
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |
138 |
|
} |
139 |
|
} |
140 |
0
|
if(mRingtone == null){ |
141 |
0
|
mRingtone = RingtoneManager.getRingtone(mBicycleApp, alert); |
142 |
|
} |
143 |
|
|
144 |
0
|
mRingtone.play(); |
145 |
|
} |
146 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0,8 |
|
147 |
0
|
public static void stopReminder(){... |
148 |
0
|
if(mRingtone != null){ |
149 |
0
|
if(mRingtone.isPlaying()){ |
150 |
0
|
mRingtone.stop(); |
151 |
|
} |
152 |
|
} |
153 |
0
|
if(mVibrator != null){ |
154 |
0
|
mVibrator.cancel(); |
155 |
|
} |
156 |
|
} |
157 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
158 |
0
|
public static void vibrate(){... |
159 |
0
|
if(mVibrator == null){ |
160 |
0
|
mVibrator = (Vibrator) mBicycleApp.getSystemService(Context.VIBRATOR_SERVICE); |
161 |
|
} |
162 |
0
|
long[] patten = {1000, 2000, 1000, 2000, 1000, 2000}; |
163 |
0
|
mVibrator.vibrate(patten, 2); |
164 |
|
} |
165 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
166 |
490
|
public static String getText(int resId){... |
167 |
490
|
return mBicycleApp.getText(resId).toString(); |
168 |
|
} |
169 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
170 |
0
|
public static Drawable getDrawable(int resId){... |
171 |
0
|
if(resId != 0){ |
172 |
0
|
return mBicycleApp.getResources().getDrawable(resId); |
173 |
|
} |
174 |
0
|
return null; |
175 |
|
} |
176 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
177 |
0
|
public static int getColor(int resId){... |
178 |
0
|
return mBicycleApp.getResources().getColor(resId); |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
184 |
0
|
public static void exitApplication(){... |
185 |
0
|
int pid = android.os.Process.myPid(); |
186 |
0
|
android.os.Process.killProcess(pid); |
187 |
|
} |
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
189 |
10
|
public static int dip2px(float dipValue){... |
190 |
10
|
final float scale = mBicycleApp.getResources().getDisplayMetrics().density; |
191 |
10
|
return (int)(dipValue * scale + 0.5f); |
192 |
|
} |
193 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
194 |
0
|
public static int px2dip(float pxValue){... |
195 |
0
|
final float scale = mBicycleApp.getResources().getDisplayMetrics().density; |
196 |
0
|
return (int)(pxValue / scale + 0.5f); |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
@return |
202 |
|
|
|
|
| 70% |
Uncovered Elements: 6 (20) |
Complexity: 7 |
Complexity Density: 0,58 |
|
203 |
10
|
public static int getNetworkInfo(){... |
204 |
10
|
int result = Constants.NetworkInfo.DISCONNECT; |
205 |
10
|
try { |
206 |
10
|
ConnectivityManager cm = (ConnectivityManager) mBicycleApp.getSystemService(Context.CONNECTIVITY_SERVICE); |
207 |
10
|
if(cm != null){ |
208 |
10
|
NetworkInfo networkInfo = cm.getActiveNetworkInfo(); |
209 |
10
|
if(networkInfo != null && networkInfo.isConnected()){ |
210 |
10
|
if(networkInfo.getState() == State.CONNECTED){ |
211 |
10
|
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){ |
212 |
10
|
result = Constants.NetworkInfo.WIFI; |
213 |
|
}else { |
214 |
0
|
result = Constants.NetworkInfo.MOBILE; |
215 |
|
} |
216 |
|
} |
217 |
|
} |
218 |
|
} |
219 |
|
} catch (Exception e) { |
220 |
0
|
e.printStackTrace(); |
221 |
|
} |
222 |
10
|
return result; |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
|
|
| 87,2% |
Uncovered Elements: 5 (39) |
Complexity: 6 |
Complexity Density: 0,18 |
|
228 |
4
|
public static boolean loadCitySetting() throws Exception{ ... |
229 |
4
|
String cityName = Utils.getStringDataFromLocal(Constants.LocalStoreTag.CITY_NAME); |
230 |
4
|
if(cityName == null || cityName.equals("")){ |
231 |
1
|
return false; |
232 |
|
} |
233 |
3
|
boolean result = false; |
234 |
3
|
try { |
235 |
3
|
InputStream inputStream = BicycleApp.getInstance().getAssets().open(Constants.CitySetting.CITY_SETTING_FILENAME); |
236 |
3
|
StringBuilder stringBuilder = new StringBuilder(); |
237 |
3
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Constants.HttpSetting.HTTP_CONT_ENCODE)); |
238 |
3
|
String line = null; |
239 |
?
|
while((line=reader.readLine()) != null){ |
240 |
399
|
stringBuilder.append(line); |
241 |
|
} |
242 |
3
|
String jsonStr = stringBuilder.toString(); |
243 |
3
|
JSONObject jsonObject = new JSONObject(jsonStr); |
244 |
3
|
JSONObject cityJson = jsonObject.getJSONObject(cityName); |
245 |
3
|
if(cityJson != null){ |
246 |
3
|
String tabs = cityJson.getString(Constants.SettingJsonTag.TABS); |
247 |
3
|
String allBicyclesUrl = cityJson.getString(Constants.SettingJsonTag.ALL_BICYCLES_URL); |
248 |
3
|
String bicycleDetailUrl = cityJson.getString(Constants.SettingJsonTag.BICYCLE_DETAIL_URL); |
249 |
3
|
double defaultLatitude = cityJson.getDouble(Constants.SettingJsonTag.DEFAULT_LATITUDE); |
250 |
3
|
double defaultLongitude = cityJson.getDouble(Constants.SettingJsonTag.DEFAULT_LONGITUDE); |
251 |
3
|
double offsetLatitude = cityJson.getDouble(Constants.SettingJsonTag.OFFSET_LATITUDE); |
252 |
3
|
double offsetLongitude = cityJson.getDouble(Constants.SettingJsonTag.OFFSET_LONGITUDE); |
253 |
3
|
String assetsFileName = cityJson.getString(Constants.SettingJsonTag.ASSETS_FILE_NAME); |
254 |
3
|
boolean showBicycleNumber = cityJson.getBoolean(Constants.SettingJsonTag.SHOW_BICYCLE_NUMBER); |
255 |
3
|
boolean refreshSingle = cityJson.getBoolean(Constants.SettingJsonTag.REFRESH_SINGLE); |
256 |
3
|
boolean needDecode = cityJson.getBoolean(Constants.SettingJsonTag.NEED_DECODE); |
257 |
3
|
int defaultZoom = cityJson.getInt(Constants.SettingJsonTag.DEFAULT_ZOOM); |
258 |
|
|
259 |
3
|
CitySetting citySetting = new CitySetting(tabs, allBicyclesUrl, bicycleDetailUrl, defaultLatitude, defaultLongitude, |
260 |
|
offsetLatitude, offsetLongitude, assetsFileName, showBicycleNumber, refreshSingle, needDecode, defaultZoom); |
261 |
3
|
GlobalSetting.getInstance().setCitySetting(citySetting); |
262 |
3
|
result = true; |
263 |
|
} |
264 |
|
} catch (Exception e) { |
265 |
0
|
e.printStackTrace(); |
266 |
0
|
throw e; |
267 |
|
} |
268 |
3
|
return result; |
269 |
|
} |
270 |
|
|
|
|
| 73,3% |
Uncovered Elements: 4 (15) |
Complexity: 3 |
Complexity Density: 0,23 |
|
271 |
3
|
public static void loadBicyclesInfoFromAssets() throws Exception{... |
272 |
3
|
AssetManager assetManager = BicycleApp.getInstance().getAssets(); |
273 |
3
|
try { |
274 |
3
|
String assetFileName = GlobalSetting.getInstance().getCitySetting().getAssetsFileName(); |
275 |
3
|
InputStream inputStream = assetManager.open(assetFileName, AssetManager.ACCESS_BUFFER); |
276 |
3
|
StringBuilder stringBuilder = new StringBuilder(); |
277 |
3
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Constants.HttpSetting.HTTP_CONT_ENCODE)); |
278 |
3
|
String line = null; |
279 |
?
|
while((line=reader.readLine()) != null){ |
280 |
3
|
stringBuilder.append(line); |
281 |
|
} |
282 |
3
|
String jsonStr = stringBuilder.toString(); |
283 |
3
|
setToDataset(jsonStr); |
284 |
|
} catch (Exception e) { |
285 |
0
|
e.printStackTrace(); |
286 |
0
|
throw e; |
287 |
|
} |
288 |
|
} |
289 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 3 |
Complexity Density: 0,19 |
|
290 |
4
|
public static void setToDataset(String jsonStr){... |
291 |
4
|
try { |
292 |
4
|
BicycleDataset dataset = BicycleDataset.getInstance(); |
293 |
4
|
JSONObject jsonObject = new JSONObject(jsonStr); |
294 |
3
|
JSONArray jsonArray = jsonObject.getJSONArray(Constants.BicycleJsonTag.STATION); |
295 |
189
|
for(int i = 0, total = jsonArray.length(); i < total; i++){ |
296 |
186
|
JSONObject jsonItem = jsonArray.getJSONObject(i); |
297 |
186
|
int id = jsonItem.getInt(Constants.BicycleJsonTag.ID); |
298 |
186
|
String name = jsonItem.getString(Constants.BicycleJsonTag.NAME); |
299 |
186
|
double latitude = jsonItem.getDouble(Constants.BicycleJsonTag.LATITUDE); |
300 |
186
|
double longitude = jsonItem.getDouble(Constants.BicycleJsonTag.LONGITUDE); |
301 |
186
|
int capacity = jsonItem.getInt(Constants.BicycleJsonTag.CAPACITY); |
302 |
186
|
int available = jsonItem.getInt(Constants.BicycleJsonTag.AVAIABLE); |
303 |
186
|
String address = jsonItem.getString(Constants.BicycleJsonTag.ADDRESS); |
304 |
|
|
305 |
|
|
306 |
186
|
BicycleStationInfo bicycleInfo = new BicycleStationInfo(id, name, latitude, longitude, capacity, available, address); |
307 |
186
|
dataset.addBicycleInfo(id, bicycleInfo); |
308 |
|
} |
309 |
|
} catch (Exception e) { |
310 |
1
|
e.printStackTrace(); |
311 |
|
} |
312 |
|
} |
313 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
314 |
2
|
public static void clearDataset(){... |
315 |
2
|
BicycleDataset.getInstance().clearData(); |
316 |
|
} |
317 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
318 |
0
|
public static String getPinyinCaptalLetter(String bicycleName){... |
319 |
0
|
return null; |
320 |
|
} |
321 |
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 3 |
Complexity Density: 0,11 |
|
322 |
0
|
public static String decodeSZCode(String str) {... |
323 |
0
|
int len = str.length() / 2; |
324 |
0
|
int i = 1; |
325 |
0
|
StringBuffer sb = new StringBuffer(); |
326 |
0
|
int key = getIntegerValue(str.charAt(0)) * 16 + getIntegerValue(str.charAt(1)); |
327 |
0
|
System.out.println("key = " + key); |
328 |
0
|
int ivalue = 0; |
329 |
0
|
int jvalue = 0; |
330 |
0
|
while (i < len) { |
331 |
0
|
ivalue = getIntegerValue(str.charAt(i * 2)) * 16 + getIntegerValue(str.charAt(i * 2 + 1)); |
332 |
0
|
ivalue = key ^ ivalue; |
333 |
0
|
System.out.println("ivalue = " + ivalue); |
334 |
0
|
if (ivalue == 2) { |
335 |
0
|
i = i + 1; |
336 |
0
|
key = Getkey(key, ivalue); |
337 |
0
|
ivalue = getIntegerValue(str.charAt(i * 2)) * 16 + getIntegerValue(str.charAt(i * 2 + 1)); |
338 |
0
|
ivalue = key ^ ivalue; |
339 |
0
|
jvalue = ivalue; |
340 |
0
|
i = i + 1; |
341 |
0
|
key = Getkey(key, ivalue); |
342 |
0
|
ivalue = getIntegerValue(str.charAt(i * 2)) * 16 + getIntegerValue(str.charAt(i * 2 + 1)); |
343 |
0
|
ivalue = key ^ ivalue; |
344 |
0
|
jvalue = jvalue * 256 + ivalue; |
345 |
0
|
sb.append((char)jvalue); |
346 |
|
} else { |
347 |
0
|
sb.append((char)ivalue); |
348 |
|
} |
349 |
0
|
i = i + 1; |
350 |
0
|
key = Getkey(key, ivalue); |
351 |
|
} |
352 |
0
|
return sb.toString(); |
353 |
|
} |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
354 |
0
|
private static int Getkey(int key, int sc) {... |
355 |
0
|
return (key * 0x0063 + sc) % 0x100; |
356 |
|
} |
357 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
358 |
0
|
private static int getIntegerValue(char ch){... |
359 |
0
|
if(ch >= 65 && ch <= 70){ |
360 |
0
|
return ch - 55; |
361 |
|
}else{ |
362 |
0
|
return ch - 48; |
363 |
|
} |
364 |
|
} |
365 |
|
|
366 |
|
} |