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