|
@@ -0,0 +1,327 @@
|
|
|
|
|
+package com.usai.apex.apexcrm.application;
|
|
|
|
|
+
|
|
|
|
|
+import android.app.Activity;
|
|
|
|
|
+import android.content.Intent;
|
|
|
|
|
+import android.content.SharedPreferences;
|
|
|
|
|
+import android.content.res.Configuration;
|
|
|
|
|
+import android.content.res.Resources;
|
|
|
|
|
+import android.os.Handler;
|
|
|
|
|
+import android.os.Looper;
|
|
|
|
|
+import android.text.TextUtils;
|
|
|
|
|
+
|
|
|
|
|
+import com.usai.apex.apexcrm.MainActivity;
|
|
|
|
|
+import com.usai.apex.apexcrm.R;
|
|
|
|
|
+import com.usai.apex.apexcrm.dataProvider.DataProvider;
|
|
|
|
|
+import com.usai.apex.apexcrm.receiver.AlarmReceiver;
|
|
|
|
|
+//import com.usai.apex.apexcrm.service.CRMService;
|
|
|
|
|
+import com.usai.apex.apexcrm.service.CRMService3;
|
|
|
|
|
+import com.usai.redant.rautils.application.RAApplication;
|
|
|
|
|
+import com.usai.redant.rautils.receiver.RABroadcast;
|
|
|
|
|
+import com.usai.redant.rautils.utils.AESUtil;
|
|
|
|
|
+import com.usai.redant.rautils.utils.RAProviderHelper;
|
|
|
|
|
+
|
|
|
|
|
+import org.json.JSONObject;
|
|
|
|
|
+
|
|
|
|
|
+public class RACRMApp extends RAApplication implements RAProviderHelper.ProviderHelperDelegate {
|
|
|
|
|
+
|
|
|
|
|
+ private final static String PreferenceKey = "ApexCRM";
|
|
|
|
|
+ private final static String SecretKey = "usai";
|
|
|
|
|
+
|
|
|
|
|
+ // region Service
|
|
|
|
|
+ public static String CHANNEL_ID = "ApexCRM";
|
|
|
|
|
+ public static String CHANNEL_NAME = "ApexCRM";
|
|
|
|
|
+ private boolean bLogin = false;
|
|
|
|
|
+
|
|
|
|
|
+ // endregion
|
|
|
|
|
+
|
|
|
|
|
+ // region File Provider
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String getProviderAuthorities() {
|
|
|
|
|
+ return "com.usai.apex.apex.crm.fileprovider";
|
|
|
|
|
+ }
|
|
|
|
|
+ // endregion
|
|
|
|
|
+
|
|
|
|
|
+ // region Activity Life
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void applicationDidEnterBackground() {
|
|
|
|
|
+ super.applicationDidEnterBackground();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void applicationDidEnterForeground() {
|
|
|
|
|
+ super.applicationDidEnterForeground();
|
|
|
|
|
+
|
|
|
|
|
+ //notification
|
|
|
|
|
+ Intent intent = new Intent(RABroadcast.ACTION_REDANT_INIT_ALARM);
|
|
|
|
|
+ intent.setClass(RACRMApp.this, AlarmReceiver.class);
|
|
|
|
|
+ intent.setPackage(getPackageName());
|
|
|
|
|
+ sendBroadcast(intent);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public Resources getResources() {
|
|
|
|
|
+// Resources resources = super.getResources();
|
|
|
|
|
+// Configuration config = resources.getConfiguration();
|
|
|
|
|
+// if(config.fontScale != 1.0f) {
|
|
|
|
|
+// config.fontScale = 1.0f;
|
|
|
|
|
+// return this.createConfigurationContext(config).getResources();
|
|
|
|
|
+// } else {
|
|
|
|
|
+// return resources;
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+@Override
|
|
|
|
|
+public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
|
+ if (newConfig.fontScale != 1)//非默认值
|
|
|
|
|
+ getResources();
|
|
|
|
|
+ super.onConfigurationChanged(newConfig);
|
|
|
|
|
+}
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Resources getResources() {
|
|
|
|
|
+ Resources res = super.getResources();
|
|
|
|
|
+ if (res.getConfiguration().fontScale != 1) {//非默认值
|
|
|
|
|
+ Configuration newConfig = new Configuration();
|
|
|
|
|
+ newConfig.setToDefaults();//设置默认
|
|
|
|
|
+ res.updateConfiguration(newConfig, res.getDisplayMetrics());
|
|
|
|
|
+ }
|
|
|
|
|
+ return res;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // endregion
|
|
|
|
|
+
|
|
|
|
|
+ // region Override
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onCreate() {
|
|
|
|
|
+ super.onCreate();
|
|
|
|
|
+
|
|
|
|
|
+ SharedPreferences pref = sharedPreferences(PreferenceKey);
|
|
|
|
|
+
|
|
|
|
|
+ user = decryptString(pref.getString("user", ""));
|
|
|
|
|
+ password = decryptString(pref.getString("password", ""));
|
|
|
|
|
+
|
|
|
|
|
+ startService(CRMService3.class);
|
|
|
|
|
+ initAlarm(AlarmReceiver.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // endregion
|
|
|
|
|
+
|
|
|
|
|
+ // region Getter
|
|
|
|
|
+
|
|
|
|
|
+ public static RACRMApp sharedCRM() {
|
|
|
|
|
+ return (RACRMApp)sharedApplication();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // endregion
|
|
|
|
|
+
|
|
|
|
|
+ // region User
|
|
|
|
|
+
|
|
|
|
|
+ private String user;
|
|
|
|
|
+ private String password;
|
|
|
|
|
+
|
|
|
|
|
+ public String getUser() {
|
|
|
|
|
+ return user;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String getPassword() {
|
|
|
|
|
+ return password;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setUser(String user) {
|
|
|
|
|
+ this.user = user;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void setPassword(String password) {
|
|
|
|
|
+ this.password = password;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void login(String user, String password) {
|
|
|
|
|
+ bLogin = true;
|
|
|
|
|
+
|
|
|
|
|
+ SharedPreferences pref = sharedPreferences(PreferenceKey);
|
|
|
|
|
+ SharedPreferences.Editor editor = pref.edit();
|
|
|
|
|
+ String key = user + "_notification_id";
|
|
|
|
|
+ try {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ editor.putString("user", encryptString(user));
|
|
|
|
|
+ editor.putString("password", encryptString(password));
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ editor.putString(key, null);
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ editor.commit();
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ setUser(user);
|
|
|
|
|
+ setPassword(password);
|
|
|
|
|
+
|
|
|
|
|
+ DataProvider.uploadDeviceId();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void logout() {
|
|
|
|
|
+ setUser(null);
|
|
|
|
|
+ setPassword(null);
|
|
|
|
|
+ bLogin = false;
|
|
|
|
|
+
|
|
|
|
|
+ SharedPreferences pref = sharedPreferences(PreferenceKey);
|
|
|
|
|
+ SharedPreferences.Editor editor = pref.edit();
|
|
|
|
|
+ String key = user + "_notification_id";
|
|
|
|
|
+ try {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ editor.remove("user");
|
|
|
|
|
+ editor.remove("password");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ editor.putString(key, null);
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ editor.commit();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean isLogin() {
|
|
|
|
|
+ return bLogin;
|
|
|
|
|
+// return user != null && password != null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean noAccount()
|
|
|
|
|
+ {
|
|
|
|
|
+ return TextUtils.isEmpty(RACRMApp.sharedCRM().getUser())||TextUtils.isEmpty(RACRMApp.sharedCRM().getPassword());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // endregion
|
|
|
|
|
+
|
|
|
|
|
+ // region Notification
|
|
|
|
|
+ private volatile long notificationID;
|
|
|
|
|
+ public void setNotificationID(long id) {
|
|
|
|
|
+ notificationID = id;
|
|
|
|
|
+
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ SharedPreferences pref = sharedPreferences(PreferenceKey);
|
|
|
|
|
+ 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(PreferenceKey);
|
|
|
|
|
+ String key = user + "_notification_id";
|
|
|
|
|
+ long id = pref.getLong(key, 0);
|
|
|
|
|
+ notificationID = id;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return notificationID;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void receiveNotificationOnMainThread(final JSONObject notification) {
|
|
|
|
|
+ new Handler(Looper.getMainLooper()).post(new Runnable() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void run() {
|
|
|
|
|
+ receiveNotification(notification);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void receiveNotification(JSONObject notification) {
|
|
|
|
|
+
|
|
|
|
|
+// Log.e("debug", "receiveNotification: "+notification.toString());
|
|
|
|
|
+ if (notification != null) {
|
|
|
|
|
+
|
|
|
|
|
+// JSONObject aps = notification.optJSONObject("aps");
|
|
|
|
|
+// if (aps != null)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+// final int id = notification.optInt("id");
|
|
|
|
|
+
|
|
|
|
|
+ final int id = notification.optInt("notificationId");
|
|
|
|
|
+ boolean isActive = !isBackground(); // 程序是否在前台
|
|
|
|
|
+ if (isActive) {
|
|
|
|
|
+ // 直接转发给网页处理
|
|
|
|
|
+ readNotificationNow(notification.toString());
|
|
|
|
|
+
|
|
|
|
|
+ // 弹窗提示
|
|
|
|
|
+// AlertDialog.Builder builder = new AlertDialog.Builder(getCurrentActivity());
|
|
|
|
|
+// builder.setTitle("Warning");
|
|
|
|
|
+// builder.setMessage("you have a message, read it now?");
|
|
|
|
|
+// builder.setNegativeButton("Cancel",null);
|
|
|
|
|
+// builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
+// readNotificationNow(id);
|
|
|
|
|
+// }
|
|
|
|
|
+// });
|
|
|
|
|
+// builder.show();
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 发通知
|
|
|
|
|
+
|
|
|
|
|
+// Log.e("debug", "not active send notification");
|
|
|
|
|
+ JSONObject alert = notification.optJSONObject("alert");
|
|
|
|
|
+ String title = null,body = null;
|
|
|
|
|
+ if (alert != null) {
|
|
|
|
|
+
|
|
|
|
|
+ title = alert.optString("title");
|
|
|
|
|
+ body = alert.optString("body");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ NotificationContent notificationContent = new NotificationContent(id, title, body, R.drawable.ic_action_bar_o, R.drawable.ic_notification_large , notification.toString());
|
|
|
|
|
+
|
|
|
|
|
+ showNotification(notificationContent, CHANNEL_ID, CHANNEL_NAME, MainActivity.class);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void readNotificationNow(String notificationstr) {
|
|
|
|
|
+
|
|
|
|
|
+ Activity activity = getCurrentActivity();
|
|
|
|
|
+
|
|
|
|
|
+ if (activity instanceof MainActivity) {
|
|
|
|
|
+
|
|
|
|
|
+ MainActivity mainActivity = (MainActivity)activity;
|
|
|
|
|
+ mainActivity.readNotificationNow(notificationstr);
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+
|
|
|
|
|
+ MainActivity.startActivityForNotification(activity, notificationstr);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // endregion
|
|
|
|
|
+
|
|
|
|
|
+ // region Private
|
|
|
|
|
+
|
|
|
|
|
+ public String encryptString(String string) {
|
|
|
|
|
+ if (TextUtils.isEmpty(string)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ return AESUtil.encrypt256(SecretKey,string);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String decryptString(String string) {
|
|
|
|
|
+ if (TextUtils.isEmpty(string)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ return AESUtil.decrypt256(SecretKey, string);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // endregion
|
|
|
|
|
+
|
|
|
|
|
+}
|