Commit 7687088692fca95aba9b7a13c968139817c6c939

Authored by wugian
1 parent 10041a18

fangTang code review

1 1 package com.xgimi.gimicinema.service;
2 2
3 3 import android.app.Service;
4   -import android.content.BroadcastReceiver;
5 4 import android.content.ComponentName;
6 5 import android.content.Context;
7 6 import android.content.Intent;
8   -import android.content.IntentFilter;
9 7 import android.content.ServiceConnection;
10 8 import android.content.SharedPreferences;
11 9 import android.os.Handler;
... ... @@ -15,59 +13,37 @@ import android.os.RemoteException;
15 13 import android.text.TextUtils;
16 14 import android.util.Log;
17 15 import android.widget.Toast;
18   -
19   -import com.android.volley.DefaultRetryPolicy;
20   -import com.android.volley.RequestQueue;
21   -import com.android.volley.Response;
22   -import com.android.volley.VolleyError;
23   -import com.android.volley.toolbox.StringRequest;
24   -import com.android.volley.toolbox.Volley;
25 16 import com.gimi.common.cinema.db.NewDBManager;
26 17 import com.gimi.common.cinema.model.BaseData;
27 18 import com.gimi.common.cinema.model.Constant;
28 19 import com.gimi.common.cinema.model.ControlMessage;
29 20 import com.gimi.common.cinema.model.GimiMessage;
30   -import com.gimi.common.cinema.model.GimiSysData;
31 21 import com.gimi.common.cinema.model.GimiSysMsg;
32   -import com.gimi.common.cinema.model.MessageEvent;
33 22 import com.gimi.common.cinema.model.PlayStatus;
34 23 import com.gimi.common.cinema.model.PlayStatusMessage;
35 24 import com.gimi.common.cinema.model.SambaMsg;
36 25 import com.gimi.common.cinema.model.UdpModel;
37 26 import com.gimi.common.cinema.utils.AuthUtils;
38   -import com.gimi.common.cinema.utils.CToast;
39 27 import com.gimi.common.cinema.utils.FolderUtils;
40 28 import com.gimi.common.cinema.utils.LocalDataUtils;
41 29 import com.gimi.common.cinema.utils.MovieMessageUtils;
42   -import com.gimi.common.cinema.utils.NetStatusUtils;
43 30 import com.gimi.common.cinema.utils.OpenMMUtils;
44 31 import com.gimi.common.cinema.utils.SambaFileCharge;
45 32 import com.gimi.common.cinema.utils.SystemUtils;
46 33 import com.gimi.common.cinema.utils.T;
47   -import com.gimi.common.cinema.utils.TimeoutCharge;
48 34 import com.gimi.common.cinema.utils.Utils;
49 35 import com.google.gson.Gson;
50 36 import com.google.gson.GsonBuilder;
51   -import com.google.gson.JsonSyntaxException;
52   -import com.umeng.analytics.MobclickAgent;
53 37 import com.xgimi.gimicinema.BuildConfig;
54 38 import com.xgimi.gimicinema.ICinemaControl;
55   -import com.xgimi.gimicinema.R;
56 39 import com.xgimi.gimicinema.activity.CinemaConfig;
57 40 import com.xgimi.gimicinema.deserializer.GimiMessageDeserializer;
58 41 import com.xgimi.gimicinema.deserializer.GimiSysMsgDeserializer;
59 42 import com.xgimi.gimicinema.mview.IAskView;
60 43 import com.xgimi.gimicinema.presenter.AskPresenter;
61 44
62   -import org.greenrobot.eventbus.EventBus;
63   -
64 45 import java.util.ArrayList;
65   -import java.util.HashMap;
66 46 import java.util.List;
67   -import java.util.TimerTask;
68   -import java.util.concurrent.Executors;
69   -import java.util.concurrent.ScheduledExecutorService;
70   -import java.util.concurrent.TimeUnit;
71 47
72 48
73 49 /**
... ... @@ -75,27 +51,10 @@ import java.util.concurrent.TimeUnit;
75 51 */
76 52 public class AskService extends Service implements IAskView {
77 53 public static final String TAG = "AskService";
78   -
79   - public static final String ASK_SERVICE_ACTION = "com.qnbar.ask.service";
80   - private static String ROOT_URL = "http://fbs.qnbar.com/";
81   - private static String ROOT_URL_FOR_TEST = "http://fbs.t.qnbar.com/";
82   - private static final String STATUS_URL = "apis/rest/Server/getStatus?imei=";
83   - private static final String MESSAGE_URL = "apis/rest/Server/getMessage?imei=";
84   -
85   - private static final int CUR_PERIOD = 15;
86   -
87 54 private Handler handler = new Handler();
88 55
89   - private int unSupport = 0;
90   -
91   - private int statusCount = 0;
92   - private int messageCount = 0;
93 56 private Context context;
94 57 private SystemUtils systemUtils;
95   - private StringRequest reqGetStatus;
96   - private StringRequest reqGetMessage;
97   - private RequestQueue mQueueSts;
98   - private RequestQueue mQueueMsg;
99 58 private SharedPreferences sharedPreferences;
100 59 private NewDBManager dbManager;
101 60 private LocalDataUtils localDataUtils;
... ... @@ -103,16 +62,9 @@ public class AskService extends Service implements IAskView {
103 62 private String ip = "";
104 63 private String folder = "";
105 64
106   - private int offlinePromoteCount = 0;
107   -
108   - private int delay = 15;
109 65 private SambaMsg sambaMsg;
110 66 private Gson gson;
111 67 private UdpPostSender sender;
112   - // private Type typeStatus;
113   -// private Type typeMessage;
114   -
115   - // 是否需要完善UDP通信机制,提高通信稳定性
116 68 @Override
117 69 public void onCreate() {
118 70 super.onCreate();
... ... @@ -120,11 +72,6 @@ public class AskService extends Service implements IAskView {
120 72 if (!mBound) {
121 73 attemptToBindService();
122 74 }
123   - initBroadcast();
124   -// typeStatus = new TypeToken<QnResult<StatusData>>() {
125   -// }.getType();
126   -// typeMessage = new TypeToken<QnResult<List<MessageData>>>() {
127   -// }.getType();
128 75 gson = new GsonBuilder()
129 76 .registerTypeAdapter(GimiSysMsg.class, new GimiSysMsgDeserializer())
130 77 .registerTypeAdapter(GimiMessage.class, new GimiMessageDeserializer())
... ... @@ -136,87 +83,11 @@ public class AskService extends Service implements IAskView {
136 83 localDataUtils = new LocalDataUtils(context);
137 84 dbManager = new NewDBManager(this);
138 85 systemUtils = new SystemUtils();
139   - mQueueSts = Volley.newRequestQueue(context);
140   - mQueueMsg = Volley.newRequestQueue(context);
141 86 sharedPreferences = Utils.getSp(context);
142   -
143   - boolean test = sharedPreferences.getBoolean("test", false);
144 87 agentType = Utils.getInt(sharedPreferences, "agent-type", 0);
145   - if (test) {
146   - ROOT_URL = ROOT_URL_FOR_TEST;
147   - }
148 88 Log.d(TAG, "onCreate: " + BuildConfig.MACHINE_TYPE);
149   - String pid = SystemUtils.getPid(context, BuildConfig.MACHINE_TYPE);
150 89 updateMsg();
151 90
152   - reqGetMessage = new StringRequest(ROOT_URL + MESSAGE_URL + pid,
153   - new Response.Listener<String>() {
154   - @Override
155   - public void onResponse(String response) {
156   - AskService.this.handlerMessage(response);
157   - }
158   - }, new Response.ErrorListener() {
159   - @Override
160   - public void onErrorResponse(VolleyError error) {
161   - Log.d(TAG, " msg error" + error.toString());
162   - }
163   - });
164   -
165   - reqGetStatus = new StringRequest(ROOT_URL + STATUS_URL + pid,
166   - new Response.Listener<String>() {
167   - @Override
168   - public void onResponse(String response) {
169   - offlinePromoteCount = 0;
170   - promoteCount = 0;
171   - if (!AuthUtils.checkOfflineAuthTime(context)) {
172   - if (!AuthUtils.isFinish(context)) {
173   - CToast.makeText(context, "店铺服务已到期,请联系相关工作人员为您续费,否则可能影响后续使用及营业", 8 * 1000).show();
174   - } else {
175   - showMsg("由于你长期没有续费,系统已暂停你的相关功能使用");
176   - }
177   - }
178   - AskService.this.handleStatus(response);
179   - }
180   - }, new Response.ErrorListener() {
181   - @Override
182   - public void onErrorResponse(VolleyError error) {
183   - Log.d(TAG, "status error:" + error.toString());
184   - boolean pingIp = NetStatusUtils.isAvailableByPing("114.55.207.168");//qnbar ip 通
185   - boolean pingUrl = TimeoutCharge.ping("fbs.qnbar.com");//qnbar addr 通
186   - boolean pingLocal = NetStatusUtils.isAvailableByPing(sambaMsg.getIp());//qnbar addr 通
187   -
188   - if (AuthUtils.checkOfflineAuthTime(context)) {
189   - if (!pingIp || !pingUrl) {
190   - openLed();
191   - }
192   - }
193   -
194   - if (offlinePromoteCount++ >= 3) {
195   - return;
196   - }
197   - String append = pingLocal ? "电影服务器正常,不影响正常观影" : "电影服务器异常请检查相应配置,否则不能正常观影";
198   - if ((!pingIp || !pingUrl)) {
199   - if (promoteCount++ < 3) {
200   - showMsg("机器不能访问远程控制数据,为不影响正常营业,已为您开机," + append);
201   - } else {
202   - Log.d(TAG, "onErrorResponse: already promote 3 times do nothing " + promoteCount);
203   - }
204   - } else {
205   - if (!pingLocal) {
206   - showMsg(append);
207   - }
208   - }
209   - }
210   - }
211   -
212   - );
213   - DefaultRetryPolicy retryPolicy = new DefaultRetryPolicy(50 * 1000,
214   - DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
215   - DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
216   -
217   - reqGetMessage.setRetryPolicy(retryPolicy);
218   - reqGetStatus.setRetryPolicy(retryPolicy);
219   -
220 91 sender = new UdpPostSender("", 53301, 53301, _handler);
221 92
222 93 askPresenter.load(context);
... ... @@ -224,100 +95,6 @@ public class AskService extends Service implements IAskView {
224 95 // PollingUtils.startPollingService(context, 60, ProguardService.class, ProguardService.STATUS_ACTION);
225 96 }
226 97
227   - int promoteCount = 0;
228   -
229   - private void handlerMessage(String response) {
230   - MessageEvent messageEvent = new MessageEvent();
231   - messageEvent.setEventId(1);
232   - messageEvent.setMessage("handle message");
233   - EventBus.getDefault().post(messageEvent);
234   - GimiSysMsg msg = null;
235   - ProguardService.lastHandlerServerMsg = System.currentTimeMillis();
236   - try {
237   - msg = gson.fromJson(response, GimiSysMsg.class);
238   - } catch (JsonSyntaxException e) {
239   - uploadCrashMsg("getMessage", response, e.getMessage());
240   - e.printStackTrace();
241   - }
242   - if (msg == null) {
243   - return;
244   - }
245   - StringBuilder buffer = new StringBuilder();
246   - if (msg.getCode() == 200) {
247   - for (GimiSysData gimiSysData : msg.getData()) {
248   - buffer.append(gimiSysData.getMessages().getMsg_content().trim()).append("\n");
249   - }
250   - if (!TextUtils.isEmpty(buffer.toString())) {
251   - if (systemUtils.getYbProp() != 3) {
252   - CToast.makeText(context, buffer.toString().trim(), 45 * 1000).show();
253   - }
254   - }
255   - saveGimiAuth(true);
256   - System.out.println(msg.toString());
257   - System.out.println(buffer.toString());
258   - } else if (msg.getCode() == 602) {
259   - setUnAuthAndToast(msg.getMessage());
260   - unSupport++;
261   - }
262   - }
263   -
264   - private void handleStatus(String response) {
265   - MessageEvent messageEvent = new MessageEvent();
266   - messageEvent.setEventId(0);
267   - messageEvent.setMessage("handle status");
268   - EventBus.getDefault().post(messageEvent);
269   -
270   - GimiMessage gimiMessage = null;
271   - ProguardService.lastHandlerServerMsg = System.currentTimeMillis();
272   - try {
273   - gimiMessage = gson.fromJson(response, GimiMessage.class);
274   - } catch (JsonSyntaxException e) {
275   - uploadCrashMsg("getStatus", response, e.getMessage());
276   - e.printStackTrace();
277   - }
278   - if (gimiMessage == null) {
279   - return;
280   - }
281   - System.out.println(gimiMessage.toString());
282   - if (gimiMessage.getCode() == 200) {
283   - saveGimiAuth(true);
284   - if (gimiMessage.getData().getStatus().equals("on")) {
285   - openLed();
286   - Constant.messageRun = true;
287   - } else {
288   - CToast.makeText(context, "您的观影时间已到,如有需要请到前台续费", 10 * 1000).show();
289   - new Handler().postDelayed(serviceRunnable, 10 * 1000);
290   - }
291   - int delayA = gimiMessage.getData().getDelay();
292   - Log.d(TAG, "delay time is :" + delayA);
293   - if (delayA != delay) {
294   - delay = delayA;
295   - Log.d(TAG, "delay time change reset");
296   - statusPool.shutdown();
297   - statusPool = null;
298   - startStatusLoop();
299   - }
300   - } else if (gimiMessage.getCode() == 602) {
301   - setUnAuthAndToast(gimiMessage.getMessage());
302   - unSupport++;
303   - }
304   - }
305   -
306   - private void uploadCrashMsg(String type, String msg, String error) {
307   - HashMap<String, String> map = new HashMap<>();
308   - map.put("imei", SystemUtils.getPid(context, BuildConfig.MACHINE_TYPE));
309   - map.put("type", type);
310   - map.put("msg", msg);
311   - map.put("error", error);
312   - MobclickAgent.onEvent(this, "handler_msg_crash", map);
313   - }
314   -
315   - private void openLed() {
316   - if (!BuildConfig.MACHINE_TYPE.equals("himedia")) {
317   - systemUtils.openLed(context);
318   - }
319   - }
320   -
321 98 private void updateMsg() {
322 99 sambaMsg = Utils.getSambaMsg(sharedPreferences);
323 100 ip = sambaMsg.getIp();
... ... @@ -326,19 +103,6 @@ public class AskService extends Service implements IAskView {
326 103
327 104 int agentType;
328 105
329   - private void setUnAuthAndToast(String msg) {
330   - saveGimiAuth(false);
331   - if (agentType == 5 && AuthUtils.checkOfflineAuthTime(this)) {
332   - return;
333   - }
334   - if (unSupport < 3) {
335   - if (systemUtils.getYbProp() != 3)
336   - CToast.makeText(context.getApplicationContext(),
337   - TextUtils.isEmpty(msg) ? getString(R.string.no_auth) : msg,
338   - 10 * 1000).show();
339   - }
340   - }
341   -
342 106 private Handler _handler = new Handler() {
343 107 @Override
344 108 public void dispatchMessage(Message msg) {
... ... @@ -348,12 +112,6 @@ public class AskService extends Service implements IAskView {
348 112 return;
349 113 }
350 114 String obj = (String) msg.obj;
351   -// UdpModel udpModel = new Gson().fromJson(obj, UdpModel.class);
352   -// if (!udpModel.isMulti()) {
353   -// playPath(getPlayUrl(udpModel.getMovie_url()));
354   -// } else {
355   - //get All list
356   -
357 115 ControlMessage controlMessage = gson.fromJson(obj, ControlMessage.class);
358 116 UdpModel udpModel1 = controlMessage.getUdpModel();
359 117 switch (controlMessage.getAction()) {
... ... @@ -379,7 +137,6 @@ public class AskService extends Service implements IAskView {
379 137 }
380 138 super.dispatchMessage(msg);
381 139 }
382   -// }
383 140 };
384 141
385 142 private void returnCurrentStatus() throws RemoteException {
... ... @@ -473,13 +230,6 @@ public class AskService extends Service implements IAskView {
473 230 systemUtils.stopMediaPlayer(context.getApplicationContext());
474 231 }
475 232
476   - private void saveGimiAuth(boolean b) {
477   - Constant.gimiAuth = b;
478   - SharedPreferences.Editor edit = sharedPreferences.edit();
479   - edit.putBoolean(Constant.GIMI_AUTHENTICATION, b);
480   - edit.apply();
481   - }
482   -
483 233 private Runnable updateRunnable = new Runnable() {
484 234 @Override
485 235 public void run() {
... ... @@ -490,39 +240,8 @@ public class AskService extends Service implements IAskView {
490 240 }
491 241 };
492 242
493   - private Runnable serviceRunnable = new Runnable() {
494   - @Override
495   - public void run() {
496   - //停止播放视频 纯后台时不走onResume,还需要启动广告视频
497   - Intent intent = new Intent(Constant.ACTION_GET_MESSAGE_CLOSE_LED);
498   - sendBroadcast(intent);
499   - Constant.messageRun = false;
500   - if (!BuildConfig.MACHINE_TYPE.equals("himedia")) {
501   - systemUtils.setLedStatus(false);
502   - }
503   - stopMediaPlayer();
504   - Intent intentHome = new Intent(Intent.ACTION_MAIN);
505   - intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //如果是服务里调用,必须加入new task标识
506   - intentHome.addCategory(Intent.CATEGORY_HOME);
507   - startActivity(intentHome);
508   - }
509   - };
510   -
511   - private Runnable closeSystem = new Runnable() {
512   - @Override
513   - public void run() {
514   - systemUtils.shutdown(context);
515   - }
516   - };
517   -
518 243 @Override
519 244 public int onStartCommand(Intent intent, int flags, int startId) {
520   -// if (systemUtils.getYbProp() != 3) {
521   -// if (agentType != 5) {
522   -// startMessageLoop();
523   -// startStatusLoop();
524   -// }
525   -// }
526 245 return START_STICKY;
527 246 }
528 247
... ... @@ -538,7 +257,6 @@ public class AskService extends Service implements IAskView {
538 257 unbindService(mServiceConnection);
539 258 mBound = false;
540 259 }
541   -// unregisterReceiver(myReceiver);
542 260 super.onDestroy();
543 261 }
544 262
... ... @@ -556,51 +274,7 @@ public class AskService extends Service implements IAskView {
556 274 public void showMsg(String msg) {
557 275 Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
558 276 }
559   -
560   - ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);
561   - ScheduledExecutorService statusPool = Executors.newScheduledThreadPool(4);
562   -
563   - void startStatusLoop() {
564   - if (statusPool == null) {
565   - statusPool = Executors.newScheduledThreadPool(4);
566   - }
567   - TimerTask task = new TimerTask() {
568   - @Override
569   - public void run() {
570   - mQueueSts.add(reqGetStatus);
571   - statusCount++;
572   - Log.d(TAG, "run: " + "statusCount = " + statusCount + ",delay = " + delay);
573   - }
574   - };
575   -// statusTimer.schedule(task, 0, delay * 1000);
576   - statusPool.scheduleAtFixedRate(task, 0, delay * 1000, TimeUnit.MILLISECONDS);
577   -// pool.
578   - }
579   -
580   - void startMessageLoop() {
581   - if (pool == null) {
582   - pool = Executors.newScheduledThreadPool(4);
583   - }
584   - TimerTask himedia = new TimerTask() {
585   - @Override
586   - public void run() {
587   - double v = ((double) (System.currentTimeMillis() / 1000 % 86400)) / 3600;
588   - if ((v > 18 && v < 24) && !Constant.messageRun && !BuildConfig.MACHINE_TYPE.equals("himedia")) {
589   - handler.postDelayed(closeSystem, 5 * 1000);
590   - }
591   - if (Constant.messageRun) {
592   - // getMessage from server
593   - mQueueMsg.add(reqGetMessage);
594   - messageCount++;
595   - }
596   -
597   - Log.d(TAG, "run: " + "messageCount = " + messageCount + ",delay = " + delay);
598   - }
599   - };
600   - pool.scheduleAtFixedRate(himedia, 0, CUR_PERIOD * 1000, TimeUnit.MILLISECONDS);
601   - }
602   -
603   - //由AIDL文件生成的Java类
  277 + //由AIDL文件生成的Java类
604 278 private ICinemaControl cinemaControl = null;
605 279
606 280 //标志当前与服务端连接状况的布尔值,false为未连接,true为连接中
... ... @@ -631,35 +305,4 @@ public class AskService extends Service implements IAskView {
631 305 }
632 306
633 307 public static final String ACTION = "cn.etzmico.broadcastreceiverregister.SENDBROADCAST";
634   - private BroadcastReceiver myReceiver = new BroadcastReceiver() {
635   -
636   - @Override
637   - public void onReceive(Context context, Intent intent) {
638   -// Toast.makeText(context, "myReceiver receive", Toast.LENGTH_SHORT)
639   -// .show();
640   - Log.d(TAG, "delay time change reset");
641   - boolean reset = intent.getBooleanExtra("reset", false);
642   - if (statusPool != null) {
643   - statusPool.shutdown();
644   - statusPool = null;
645   - }
646   - if (pool != null) {
647   - pool.shutdown();
648   - pool = null;
649   - }
650   - if (reset) {
651   - statusCount = 0;
652   - messageCount = 0;
653   - startStatusLoop();
654   - startMessageLoop();
655   - }
656   - }
657   - };
658   -
659   - private void initBroadcast() {
660   - IntentFilter filter = new IntentFilter();
661   - filter.addAction(ACTION);
662   - filter.setPriority(Integer.MAX_VALUE);
663   - registerReceiver(myReceiver, filter);
664   - }
665 308 }
... ...
Please register or login to post a comment