Parcourir la source

1.修改Android Apex Drivers根据上传状态改变首页上传按钮状态。

Pen Li il y a 7 ans
Parent
commit
f29a8e583d

+ 30 - 2
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/upload/RAUploadManager.java

@@ -1,6 +1,7 @@
 package com.usai.redant.rautils.upload;
 
 import android.content.Context;
+import android.content.Intent;
 import android.content.SharedPreferences;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
@@ -75,6 +76,9 @@ public class RAUploadManager {
         //TaskStatusCancel,
     }
 
+    public static final String UPLOAD_COUNT_CHANGED_NOTIFICATION = "com.usai.upload.count.change.notification";
+    public static final String UPLOAD_FINISH_TASK_NOTIFICATION = "com.usai.upload.finish_task.notification";
+
     public static class RAUploadManagerConfigure {
 
         public Boolean autoStart=false;
@@ -323,8 +327,14 @@ public class RAUploadManager {
                 ///////////////////////////////////////////////
 
                 boolean removefromlist = false;
-                if (status==TaskStatus.TaskStatusFinish&&removeFinish)
-                    removefromlist=true;
+                if (status==TaskStatus.TaskStatusFinish&&removeFinish) {
+                    removefromlist = true;
+
+                    // 发送Task完成通知
+                    Intent intent = new Intent(UPLOAD_FINISH_TASK_NOTIFICATION);
+                    intent.putExtra("task",taskinfo);
+                    applicationContext.sendBroadcast(intent);
+                }
                 else if (status==TaskStatus.TaskStatusError&&removeError)
                     removefromlist=true;
 
@@ -403,6 +413,12 @@ public class RAUploadManager {
         // 此处缺少读取持久话保存任务队列的实现。
     }
 
+    private void taskCountChanged() {
+
+        Intent intent = new Intent(UPLOAD_COUNT_CHANGED_NOTIFICATION);
+        applicationContext.sendBroadcast(intent);
+    }
+
     public void addTask(Bundle task) {
 
         synchronized (this) {
@@ -412,6 +428,9 @@ public class RAUploadManager {
             saveTasks();
             Log.d("_synchronized", "addTask: end" + (System.currentTimeMillis() - timeStart));
         }
+
+        taskCountChanged();
+
         if (newtaskStatus == TaskStatus.TaskStatusWait) {
             startTask(task);
         }
@@ -430,6 +449,9 @@ public class RAUploadManager {
             Log.d("_synchronized", "addTasks: end" + (System.currentTimeMillis() - timeStart));
 
         }
+
+        taskCountChanged();
+
         if (newtaskStatus == TaskStatus.TaskStatusWait) {
             for (Bundle task : tasks) {
                 startTask(task);
@@ -521,6 +543,9 @@ public class RAUploadManager {
             Log.d("_synchronized", "removeTask");
             long timeStart = System.currentTimeMillis();
             arr_queue.remove(task);
+
+            taskCountChanged();
+
             saveTasks();
 
 
@@ -550,6 +575,9 @@ public class RAUploadManager {
             Log.d("_synchronized", "removeTasks: ");
             long timeStart = System.currentTimeMillis();
             arr_queue.removeAll(tasks);
+
+            taskCountChanged();
+
             saveTasks();
             for (Bundle task : tasks) {
 

+ 7 - 0
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/ApexDriverApplication.java

@@ -226,6 +226,13 @@ public class ApexDriverApplication extends Application {
 
     }
 
+    public int getUploadCount() {
+        if (mService != null) {
+            return mService.getUploadCount();
+        }
+        return 0;
+    }
+
     void initAlarm()
     {
         Log.d("ApexDriverApplication", "onCreate: SEND" + RABroadcast.ACTION_REDANT_INIT_ALARM);

+ 52 - 0
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/ApexDriversBackgroundService.java

@@ -1,12 +1,16 @@
 package com.usai.redant.apexdrivers;
 
 import android.app.Notification;
+import android.content.BroadcastReceiver;
+import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.location.Location;
 import android.util.Log;
 
 import com.usai.redant.rautils.receiver.RABroadcast;
 import com.usai.redant.rautils.service.RAService;
+import com.usai.redant.rautils.upload.RAUploadManager;
 import com.usai.redant.rautils.utils.dbgUtil;
 
 import org.json.JSONObject;
@@ -20,6 +24,36 @@ public class ApexDriversBackgroundService extends RAService implements RAService
 
     }
 
+    private BroadcastReceiver serviceReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+
+            String action = intent.getAction();
+            if (action.equals(RAUploadManager.UPLOAD_COUNT_CHANGED_NOTIFICATION)) {
+
+                sendUploadCountChangeNotification();
+
+            } else if (action.equals(RAUploadManager.UPLOAD_FINISH_TASK_NOTIFICATION)) {
+
+                sendUploadCountChangeNotification();
+            }
+        }
+    };
+
+    public final static String APEX_DRIVER_UPLOAD_COUNT_CHANGE_NOTIFICATION = "com.usai.apex.driver.upload_count_change";
+    private void sendUploadCountChangeNotification() {
+
+        Intent intent = new Intent(APEX_DRIVER_UPLOAD_COUNT_CHANGE_NOTIFICATION);
+        intent.putExtra("upload_count",getUploadCount());
+
+        sendBroadcast(intent);
+    }
+
+    public int getUploadCount() {
+
+        return getUploadManager().get_arr_queue().size();
+    }
+
     @Override
     protected void Setup() {
 //        service_flag=FLAG_SERVICE_UPLOAD|FLAG_SERVICE_NOTIFICATION|FLAG_SERVICE_LOCATION;
@@ -28,9 +62,25 @@ public class ApexDriversBackgroundService extends RAService implements RAService
         CHANNEL_NAME=ApexDriverApplication.sharedApplication().CHANNEL_NAME;
         initServiceLocation(this);
 //        initServiceNotification(this,"replace this string with notification checking url");
+
+        RAUploadManager.configureUploadManager(getApplicationContext(), new RAUploadManager.configureBlock() {
+            @Override
+            public void configure(RAUploadManager.RAUploadManagerConfigure configure) {
+
+                configure.removeFinish = true;
+                configure.maxRetry = 10;
+            }
+        });
+
         initServiceUpload(this);
 
         dbgUtil.fileLog(this,"ApexDriversBackgroundService Setup()");
+
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(RAUploadManager.UPLOAD_COUNT_CHANGED_NOTIFICATION);
+        intentFilter.addAction(RAUploadManager.UPLOAD_FINISH_TASK_NOTIFICATION);
+
+        registerReceiver(serviceReceiver,intentFilter);
     }
 
 //    @Override
@@ -81,4 +131,6 @@ public class ApexDriversBackgroundService extends RAService implements RAService
 ////        throw new UnsupportedOperationException("Not yet implemented");
 //        return super.onBind(intent);
 //    }
+
+
 }

+ 34 - 1
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/MainActivity.java

@@ -7,9 +7,11 @@ import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.ProgressDialog;
+import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.pm.PackageManager;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
@@ -45,6 +47,8 @@ import org.json.JSONObject;
 import java.lang.ref.WeakReference;
 import java.util.Iterator;
 
+import static com.usai.redant.apexdrivers.ApexDriversBackgroundService.APEX_DRIVER_UPLOAD_COUNT_CHANGE_NOTIFICATION;
+
 //import android.location.Location;
 
 
@@ -133,6 +137,23 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 //        int a = 0;
     }
 
+    private BroadcastReceiver homeReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+
+            String action = intent.getAction();
+            if (action.equals(APEX_DRIVER_UPLOAD_COUNT_CHANGE_NOTIFICATION)) {
+
+                runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        invalidateOptionsMenu();
+                    }
+                });
+            }
+        }
+    };
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
 
@@ -161,6 +182,11 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
 
         checkNotificationEnable();
         checkNotification();
+
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(APEX_DRIVER_UPLOAD_COUNT_CHANGE_NOTIFICATION);
+
+        registerReceiver(homeReceiver,intentFilter);
     }
 
     @Override
@@ -573,7 +599,14 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
             MenuItem uploadItem = menu.add(0, 0, 0, getString(R.string.main_menu_upload_title));
 
             Drawable drawable = getResources().getDrawable(R.drawable.upload_list);
-            ImageUtil.renderingDrawable(drawable,getResources(),R.color.ApexDriverWhite);
+
+            int uploadCount = ApexDriverApplication.sharedApplication().getUploadCount();
+            if (uploadCount > 0) {
+                ImageUtil.renderingDrawable(drawable,getResources(),R.color.ApexDriverGreen);
+            } else {
+                ImageUtil.renderingDrawable(drawable,getResources(),R.color.ApexDriverWhite);
+            }
+
             uploadItem.setIcon(drawable);
 
             uploadItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

+ 1 - 0
ApexDrivers/apexdriverslib/src/main/res/values/colors.xml

@@ -27,5 +27,6 @@
     <color name="ApexDriverRedColor">#AE2838</color>
     <color name="ApexDriverTextRedColor">#ff0000</color>
     <color name="ApexDriverTextBlackColor">#000000</color>
+    <color name="ApexDriverGreen">#299D4D</color>
 
 </resources>