optimise init process.

This commit is contained in:
Hevin 2016-04-20 12:50:26 +08:00
parent 404f19037e
commit 3bd75e0799

View File

@ -2,6 +2,7 @@ package cn.jpush.phonegap;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import __PACKAGE_NAME__.R;
@ -89,7 +90,7 @@ public class JPushPlugin extends CordovaPlugin {
Log.i(TAG, "---------------- initialize" + "-" + openNotificationAlert
+ "-" + notificationAlert);
cordovaActivity = this.cordova.getActivity();
cordovaActivity = cordova.getActivity();
//如果同时缓存了打开事件 openNotificationAlert 消息事件 notificationAlert只向 UI 发打开事件
//这样做是为了和 iOS 统一
@ -108,7 +109,7 @@ public class JPushPlugin extends CordovaPlugin {
Log.i(TAG, "---------------- onPause");
shouldCacheMsg = true;
if (isStatisticsOpened && multitasking) {
JPushInterface.onPause(cordovaActivity);
JPushInterface.onPause(this.cordova.getActivity());
}
}
@ -117,7 +118,7 @@ public class JPushPlugin extends CordovaPlugin {
Log.i(TAG, "---------------- onResume" + "-" + openNotificationAlert
+ "-" + notificationAlert);
if (isStatisticsOpened && multitasking) {
JPushInterface.onResume(cordovaActivity);
JPushInterface.onResume(this.cordova.getActivity());
}
if (openNotificationAlert != null) {
notificationAlert = null;
@ -138,13 +139,18 @@ public class JPushPlugin extends CordovaPlugin {
JSONObject jExtras = new JSONObject();
for (Entry<String, Object> entry : extras.entrySet()) {
if (entry.getKey().equals("cn.jpush.android.EXTRA")) {
JSONObject jo = new JSONObject((String) entry.getValue());
JSONObject jo = null;
if (TextUtils.isEmpty((String) entry.getValue())) {
jo = new JSONObject();
} else {
jo = new JSONObject((String) entry.getValue());
String key;
Iterator keys = jo.keys();
while(keys.hasNext()) {
while (keys.hasNext()) {
key = keys.next().toString();
jExtras.put(key, jo.getString(key));
}
}
jExtras.put("cn.jpush.android.EXTRA", jo);
} else {
jExtras.put(entry.getKey(), entry.getValue());
@ -168,13 +174,18 @@ public class JPushPlugin extends CordovaPlugin {
JSONObject jExtras = new JSONObject();
for (Entry<String, Object> entry : extras.entrySet()) {
if (entry.getKey().equals("cn.jpush.android.EXTRA")) {
JSONObject jo = new JSONObject((String) entry.getValue());
JSONObject jo = null;
if (TextUtils.isEmpty((String) entry.getValue())) {
jo = new JSONObject();
} else {
jo = new JSONObject((String) entry.getValue());
String key;
Iterator keys = jo.keys();
while(keys.hasNext()) {
while (keys.hasNext()) {
key = keys.next().toString();
jExtras.put(key, jo.getString(key));
}
}
jExtras.put("cn.jpush.android.EXTRA", jo);
} else {
jExtras.put(entry.getKey(), entry.getValue());
@ -262,7 +273,7 @@ public class JPushPlugin extends CordovaPlugin {
}
void init(JSONArray data, CallbackContext callbackContext) {
JPushInterface.init(cordovaActivity.getApplicationContext());
JPushInterface.init(this.cordova.getActivity().getApplicationContext());
}
void setDebugMode(JSONArray data, CallbackContext callbackContext) {
@ -277,18 +288,18 @@ public class JPushPlugin extends CordovaPlugin {
}
void stopPush(JSONArray data, CallbackContext callbackContext) {
JPushInterface.stopPush(cordovaActivity.getApplicationContext());
JPushInterface.stopPush(this.cordova.getActivity().getApplicationContext());
callbackContext.success();
}
void resumePush(JSONArray data, CallbackContext callbackContext) {
JPushInterface.resumePush(cordovaActivity.getApplicationContext());
JPushInterface.resumePush(this.cordova.getActivity().getApplicationContext());
callbackContext.success();
}
void isPushStopped(JSONArray data, CallbackContext callbackContext) {
boolean isStopped = JPushInterface.isPushStopped(
cordovaActivity.getApplicationContext());
this.cordova.getActivity().getApplicationContext());
if (isStopped) {
callbackContext.success(1);
} else {
@ -306,55 +317,56 @@ public class JPushPlugin extends CordovaPlugin {
}
if (num != -1) {
JPushInterface.setLatestNotificationNumber(
cordovaActivity.getApplicationContext(), num);
this.cordova.getActivity().getApplicationContext(), num);
} else {
callbackContext.error("error num");
}
}
void setPushTime(JSONArray data, CallbackContext callbackContext) {
Set<Integer> days = null;
Set<Integer> days = new HashSet<Integer>();
JSONArray dayArray;
int startHour = -1;
int endHour = -1;
try {
dayArray = data.isNull(0) ? null : data.getJSONArray(0);
if (dayArray != null) {
days = new HashSet<Integer>();
dayArray = data.getJSONArray(0);
for (int i = 0; i < dayArray.length(); i++) {
days.add(dayArray.getInt(i));
}
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("error reading days json");
}
try {
startHour = data.getInt(1);
endHour = data.getInt(2);
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error("error reading data json");
callbackContext.error("error reading hour json");
}
Context context = cordovaActivity.getApplicationContext();
Context context = this.cordova.getActivity().getApplicationContext();
JPushInterface.setPushTime(context, days, startHour, endHour);
callbackContext.success();
}
void getRegistrationID(JSONArray data, CallbackContext callbackContext) {
Context context = cordovaActivity.getApplicationContext();
Context context = this.cordova.getActivity().getApplicationContext();
String regID = JPushInterface.getRegistrationID(context);
callbackContext.success(regID);
}
void onResume(JSONArray data, CallbackContext callbackContext) {
JPushInterface.onResume(cordovaActivity);
JPushInterface.onResume(this.cordova.getActivity());
}
void onPause(JSONArray data, CallbackContext callbackContext) {
JPushInterface.onPause(cordovaActivity);
JPushInterface.onPause(this.cordova.getActivity());
}
void reportNotificationOpened(JSONArray data, CallbackContext callbackContext) {
try {
String msgID;
msgID = data.getString(0);
JPushInterface.reportNotificationOpened(cordovaActivity, msgID);
JPushInterface.reportNotificationOpened(this.cordova.getActivity(), msgID);
} catch (JSONException e) {
e.printStackTrace();
}
@ -366,7 +378,7 @@ public class JPushPlugin extends CordovaPlugin {
for (int i = 0; i < data.length(); i++) {
tags.add(data.getString(i));
}
JPushInterface.setTags(cordovaActivity.getApplicationContext(),
JPushInterface.setTags(this.cordova.getActivity().getApplicationContext(),
tags, mTagWithAliasCallback);
callbackContext.success();
} catch (JSONException e) {
@ -378,7 +390,7 @@ public class JPushPlugin extends CordovaPlugin {
void setAlias(JSONArray data, CallbackContext callbackContext) {
try {
String alias = data.getString(0);
JPushInterface.setAlias(cordovaActivity.getApplicationContext(),
JPushInterface.setAlias(this.cordova.getActivity().getApplicationContext(),
alias, mTagWithAliasCallback);
callbackContext.success();
} catch (JSONException e) {
@ -396,7 +408,7 @@ public class JPushPlugin extends CordovaPlugin {
for (int i = 0; i < tagsArray.length(); i++) {
tags.add(tagsArray.getString(i));
}
JPushInterface.setAliasAndTags(cordovaActivity.getApplicationContext(),
JPushInterface.setAliasAndTags(this.cordova.getActivity().getApplicationContext(),
alias, tags, mTagWithAliasCallback);
callbackContext.success();
} catch (JSONException e) {
@ -408,7 +420,7 @@ public class JPushPlugin extends CordovaPlugin {
void setBasicPushNotificationBuilder(JSONArray data,
CallbackContext callbackContext) {
BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(
cordovaActivity);
this.cordova.getActivity());
builder.developerArg0 = "Basic builder 1";
JPushInterface.setPushNotificationBuilder(1, builder);
JSONObject obj = new JSONObject();
@ -422,7 +434,7 @@ public class JPushPlugin extends CordovaPlugin {
void setCustomPushNotificationBuilder(JSONArray data,
CallbackContext callbackContext) {
CustomPushNotificationBuilder builder = new CustomPushNotificationBuilder(
cordovaActivity, R.layout.test_notification_layout,
this.cordova.getActivity(), R.layout.test_notification_layout,
R.id.icon, R.id.title, R.id.text);
builder.developerArg0 = "Custom Builder 1";
JPushInterface.setPushNotificationBuilder(2, builder);
@ -435,7 +447,7 @@ public class JPushPlugin extends CordovaPlugin {
}
void clearAllNotification(JSONArray data, CallbackContext callbackContext) {
JPushInterface.clearAllNotifications(cordovaActivity);
JPushInterface.clearAllNotifications(this.cordova.getActivity());
}
void clearNotificationById(JSONArray data, CallbackContext callbackContext) {
@ -447,7 +459,7 @@ public class JPushPlugin extends CordovaPlugin {
callbackContext.error("error reading id json");
}
if (notificationId != -1) {
JPushInterface.clearNotificationById(cordovaActivity, notificationId);
JPushInterface.clearNotificationById(this.cordova.getActivity(), notificationId);
} else {
callbackContext.error("error id");
}
@ -474,17 +486,17 @@ public class JPushPlugin extends CordovaPlugin {
ln.setBroadcastTime(System.currentTimeMillis() + broadcastTime);
ln.setExtras(extras.toString());
JPushInterface.addLocalNotification(cordovaActivity, ln);
JPushInterface.addLocalNotification(this.cordova.getActivity(), ln);
}
void removeLocalNotification(JSONArray data, CallbackContext callbackContext)
throws JSONException {
int notificationID = data.getInt(0);
JPushInterface.removeLocalNotification(cordovaActivity, notificationID);
JPushInterface.removeLocalNotification(this.cordova.getActivity(), notificationID);
}
void clearLocalNotifications(JSONArray data, CallbackContext callbackContext) {
JPushInterface.clearLocalNotifications(cordovaActivity);
JPushInterface.clearLocalNotifications(this.cordova.getActivity());
}
/**
@ -512,11 +524,11 @@ public class JPushPlugin extends CordovaPlugin {
callbackContext.error("开始时间数值错误");
return;
}
if(!isValidHour(endHour) || !isValidMinute(endMinute)) {
if (!isValidHour(endHour) || !isValidMinute(endMinute)) {
callbackContext.error("结束时间数值错误");
return;
}
JPushInterface.setSilenceTime(cordovaActivity, startHour, startMinute,
JPushInterface.setSilenceTime(this.cordova.getActivity(), startHour, startMinute,
endHour, endMinute);
} catch (JSONException e) {
e.printStackTrace();
@ -537,7 +549,7 @@ public class JPushPlugin extends CordovaPlugin {
* http://docs.Push.io/client/android_api/#android-60
*/
void requestPermission(JSONArray data, CallbackContext callbackContext) {
JPushInterface.requestPermission(cordovaActivity);
JPushInterface.requestPermission(this.cordova.getActivity());
}
private final TagAliasCallback mTagWithAliasCallback = new TagAliasCallback() {
@ -554,7 +566,7 @@ public class JPushPlugin extends CordovaPlugin {
final String jsEvent = String.format(
"cordova.fireDocumentEvent('jpush.setTagsWithAlias',%s)",
data.toString());
cordovaActivity.runOnUiThread(new Runnable() {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
instance.webView.loadUrl("javascript:" + jsEvent);