Procházet zdrojové kódy

1.修改Android Apex Drivers推送令牌上传以及索引存取。

Pen Li před 7 roky
rodič
revize
5c1573d5b9

+ 6 - 6
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/utils/Network.java

@@ -37,12 +37,12 @@ import java.util.UUID;
 
 public class Network {
 
-    private static final int	REQUEST_TIMEOUT					= 15 * 1000;			// request
+    public static final int	REQUEST_TIMEOUT					= 15 * 1000;			// request
 
-    private static final int	SO_TIMEOUT						= 15 * 1000;			// so
+    public static final int	SO_TIMEOUT						= 15 * 1000;			// so
 
 
-    private static final int	UPLOAD_TIMEOUT						= 30 * 1000;
+    public static final int	UPLOAD_TIMEOUT						= 30 * 1000;
     public static int			AP_USER_AUTH					= 1;
     public static int			AP_USER_NOT_AUTH				= 2;
     public static int			AP_USER_NOT_EXIST				= 3;
@@ -61,7 +61,7 @@ public class Network {
     public static final int		RESULT_SESSION_EXPIRED			= -13;
     public static final int		RESULT_VER_LOW					= -15;
 
-    public static String getJson(String url, Bundle parms)
+    public static String getJson(String url, Bundle parms,int timeout)
     {
         String TAG = "net_dbg@GetJson";
         Log.d(TAG, "entry");
@@ -75,8 +75,8 @@ public class Network {
             URL _url;
             _url = new URL(url);
             connection = (HttpURLConnection) _url.openConnection();
-            connection.setReadTimeout(SO_TIMEOUT);
-            connection.setConnectTimeout(SO_TIMEOUT);
+            connection.setReadTimeout(timeout);
+            connection.setConnectTimeout(timeout);
             // 设置请求方式
             connection.setRequestMethod("POST");
             // 设置编码格式

+ 41 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/ApexDriverApplication.java

@@ -26,6 +26,7 @@ import com.usai.redant.rautils.receiver.RABroadcast;
 import com.usai.redant.rautils.utils.AESUtil;
 import com.usai.redant.rautils.utils.FileManager;
 import com.usai.redant.rautils.utils.ImageUtil;
+import com.usai.redant.rautils.utils.RAUtil;
 
 import java.io.File;
 import java.io.FileOutputStream;
@@ -41,6 +42,7 @@ public class ApexDriverApplication extends Application {
 
     public String user;
     public String password;
+    private volatile long notificationID;
 
 
     public final static int BackgroundReportTypeNone = 0;
@@ -70,6 +72,11 @@ public class ApexDriverApplication extends Application {
 
             loadBackgroundReportType();
 
+            if (user != null) {
+                // 上传Token
+                Network.uploadToken(RAUtil.getDeviceId(getApplicationContext()));
+            }
+
             Log.d("ApexDriverApplication", "onCreate: u:"+user+" p:"+password);
             mServiceConnection = new ServiceConnection() {
                 @Override
@@ -224,6 +231,40 @@ public class ApexDriverApplication extends Application {
         return p;
     }
 
+    public void setNotificationID(long id) {
+        notificationID = id;
+
+        if (user == null) {
+            return;
+        }
+
+        SharedPreferences pref = sharedPreferences();
+        SharedPreferences.Editor editor = pref.edit();
+        String key = user + "_notification_id";
+        try {
+
+            editor.putLong(key, notificationID);
+
+        } catch (Exception e) {
+            editor.putString(key, null);
+            e.printStackTrace();
+        }
+        editor.commit();
+    }
+
+    public long getNotificationID() {
+
+        if (notificationID == 0) {
+
+            SharedPreferences pref=sharedPreferences();
+            String key = user + "_notification_id";
+            long id = pref.getLong(key, 0);
+            notificationID = id;
+        }
+
+        return notificationID;
+    }
+
     public String encryptPassword() {
          if (password != null) {
              return encryptString(password);

+ 4 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/MainActivity.java

@@ -272,6 +272,10 @@ public class MainActivity extends BasicActivity implements LoginFragment.LoginCa
         setupLoginAppearance();
 
 //        restart();
+
+        // notification token
+        Network.uploadToken(RAUtil.getDeviceId(getApplicationContext()));
+
     }
 
 

+ 4 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/message/MessageActivity.java

@@ -342,6 +342,10 @@ public class MessageActivity extends BasicActivity implements AbsListView.OnScro
                         }
 
                         JSONArray messages = json.optJSONArray("messages");
+
+                        long notificationId = json.optLong("notificationId");
+                        ApexDriverApplication.sharedApplication().setNotificationID(notificationId);
+
                         handleJsonArray(messages);
 
                     } else {

+ 46 - 0
ApexDrivers/app/src/main/java/com/usai/redant/apexdrivers/network/Network.java

@@ -8,6 +8,7 @@ import android.text.TextUtils;
 
 
 import com.usai.redant.apexdrivers.ApexDriverApplication;
+import com.usai.redant.rautils.utils.RAUtil;
 
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -34,6 +35,8 @@ public class Network extends com.usai.redant.rautils.utils.Network {
     public static final String URL_DRIVER_AVAILABLE = URL_HOST + "/j/mobile/updateDriverAvailable.mo/";
     public static final String URL_CHANGE_PASSWORD = URL_HOST + "/j/mobile/changePassword.mo/";
     public static final String URL_MESSAGE_LIST = URL_HOST + "/j/mobile/moreDriverMessages.mo/";
+    public static final String URL_UPLOAD_TOKEN = URL_HOST + "/j/mobile/uploadToken.mo/";
+    public static final String URL_PULL_NOTIFICATION = URL_HOST + "/j/mobile/pullNotification.mo/";
 
     private static void prepareParams(Bundle params) {
         if (params == null) {
@@ -74,6 +77,14 @@ public class Network extends com.usai.redant.rautils.utils.Network {
         return com.usai.redant.rautils.utils.Network.isNetworkAvailable(application);
     }
 
+    public static String getJson(String url, Bundle parms) {
+        return getJson(url,parms, com.usai.redant.rautils.utils.Network.SO_TIMEOUT);
+    }
+
+    public static String getJSON(String url, Bundle params, int timeout) {
+        return getJson(url,params,timeout);
+    }
+
     public static JSONObject retrieve_pass(Application application, String user, String email) {
 
         Bundle parms = new Bundle();
@@ -357,4 +368,39 @@ public class Network extends com.usai.redant.rautils.utils.Network {
         }
     }
 
+    /**
+     * @brief 上传推送令牌,在登陆成功的时候 以及 程序启动时自动登陆的情况下
+     * */
+    public static JSONObject uploadToken(String token) {
+
+        Bundle params = new Bundle();
+        if (token != null) {
+            params.putString("token",token);
+        }
+
+        prepareParams(params);
+        String jsonStr = getJson(URL_UPLOAD_TOKEN,params);
+
+        return handleJson(jsonStr);
+
+    }
+
+    public static JSONObject pullNotification() {
+
+        Bundle params = new Bundle();
+        // token
+        params.putString("token", RAUtil.getDeviceId(ApexDriverApplication.sharedApplication().getApplicationContext()));
+
+        // id
+        long id = ApexDriverApplication.sharedApplication().getNotificationID();
+        params.putLong("id",id);
+
+        prepareParams(params);
+
+        int timeout = 5 * 1000;
+        String jsonStr = getJSON(URL_UPLOAD_TOKEN,params,timeout);
+
+        return handleJson(jsonStr);
+    }
+
 }