优化代码

This commit is contained in:
Hevin 2016-03-12 22:46:59 +08:00
parent 032b6f5d0c
commit 40c98f4f04

View File

@ -4,6 +4,7 @@ import android.app.Activity;
import android.app.Notification; import android.app.Notification;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Intent; import android.content.Intent;
import android.content.Context;
import android.util.Log; import android.util.Log;
import __PACKAGE_NAME__.R; import __PACKAGE_NAME__.R;
@ -82,7 +83,9 @@ public class JPushPlugin extends CordovaPlugin {
public void initialize(CordovaInterface cordova, CordovaWebView webView) { public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView); super.initialize(cordova, webView);
Log.i(TAG, "---------------- initialize" + "-" + JPushPlugin.openNotificationAlert + "-" + JPushPlugin.notificationAlert); Log.i(TAG, "---------------- initialize" + "-"
+ JPushPlugin.openNotificationAlert + "-"
+ JPushPlugin.notificationAlert);
cordovaActivity = this.cordova.getActivity(); cordovaActivity = this.cordova.getActivity();
@ -104,15 +107,17 @@ public class JPushPlugin extends CordovaPlugin {
Log.i(TAG, "---------------- onPause"); Log.i(TAG, "---------------- onPause");
shouldCacheMsg = true; shouldCacheMsg = true;
if (isStatisticsOpened) { if (isStatisticsOpened) {
JPushInterface.onPause(this.cordova.getActivity()); JPushInterface.onPause(cordovaActivity);
} }
} }
public void onResume(boolean multitasking) { public void onResume(boolean multitasking) {
shouldCacheMsg = false; shouldCacheMsg = false;
Log.i(TAG, "---------------- onResume" + "-" + JPushPlugin.openNotificationAlert + "-" + JPushPlugin.notificationAlert); Log.i(TAG, "---------------- onResume" + "-"
+ JPushPlugin.openNotificationAlert + "-"
+ JPushPlugin.notificationAlert);
if (isStatisticsOpened) { if (isStatisticsOpened) {
JPushInterface.onResume(this.cordova.getActivity()); JPushInterface.onResume(cordovaActivity);
} }
if (JPushPlugin.openNotificationAlert != null) { if (JPushPlugin.openNotificationAlert != null) {
JPushPlugin.notificationAlert = null; JPushPlugin.notificationAlert = null;
@ -245,7 +250,7 @@ public class JPushPlugin extends CordovaPlugin {
} }
void init(JSONArray data, CallbackContext callbackContext) { void init(JSONArray data, CallbackContext callbackContext) {
JPushInterface.init(this.cordova.getActivity().getApplicationContext()); JPushInterface.init(cordovaActivity.getApplicationContext());
//callbackContext.success(); //callbackContext.success();
} }
@ -268,18 +273,18 @@ public class JPushPlugin extends CordovaPlugin {
} }
void stopPush(JSONArray data, CallbackContext callbackContext) { void stopPush(JSONArray data, CallbackContext callbackContext) {
JPushInterface.stopPush(this.cordova.getActivity().getApplicationContext()); JPushInterface.stopPush(cordovaActivity.getApplicationContext());
callbackContext.success(); callbackContext.success();
} }
void resumePush(JSONArray data, CallbackContext callbackContext) { void resumePush(JSONArray data, CallbackContext callbackContext) {
JPushInterface.resumePush(this.cordova.getActivity().getApplicationContext()); JPushInterface.resumePush(cordovaActivity.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(
this.cordova.getActivity().getApplicationContext()); cordovaActivity.getApplicationContext());
if (isStopped) { if (isStopped) {
callbackContext.success(1); callbackContext.success(1);
} else { } else {
@ -297,7 +302,7 @@ public class JPushPlugin extends CordovaPlugin {
} }
if (num != -1) { if (num != -1) {
JPushInterface.setLatestNotificationNumber( JPushInterface.setLatestNotificationNumber(
this.cordova.getActivity().getApplicationContext(), num); cordovaActivity.getApplicationContext(), num);
} else { } else {
callbackContext.error("error num"); callbackContext.error("error num");
} }
@ -323,30 +328,30 @@ public class JPushPlugin extends CordovaPlugin {
} catch (JSONException e) { } catch (JSONException e) {
callbackContext.error("error reading hour json"); callbackContext.error("error reading hour json");
} }
Context context = this.cordova.getActivity().getApplicationContext(); Context context = cordovaActivity.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 = this.cordova.getActivity().getApplicationContext(); Context context = cordovaActivity.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(this.cordova.getActivity()); JPushInterface.onResume(cordovaActivity);
} }
void onPause(JSONArray data, CallbackContext callbackContext) { void onPause(JSONArray data, CallbackContext callbackContext) {
JPushInterface.onPause(this.cordova.getActivity()); JPushInterface.onPause(cordovaActivity);
} }
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(this.cordova.getActivity(), msgID); JPushInterface.reportNotificationOpened(cordovaActivity, msgID);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -358,8 +363,8 @@ 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(this.cordova.getActivity() JPushInterface.setTags(cordovaActivity.getApplicationContext(),
.getApplicationContext(), tags, mTagWithAliasCallback); tags, mTagWithAliasCallback);
callbackContext.success(); callbackContext.success();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -370,8 +375,8 @@ 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(this.cordova.getActivity() JPushInterface.setAlias(cordovaActivity.getApplicationContext(),
.getApplicationContext(), alias, mTagWithAliasCallback); alias, mTagWithAliasCallback);
callbackContext.success(); callbackContext.success();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -388,8 +393,8 @@ 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(this.cordova.getActivity() JPushInterface.setAliasAndTags(cordovaActivity.getApplicationContext(),
.getApplicationContext(), alias, tags, mTagWithAliasCallback); alias, tags, mTagWithAliasCallback);
callbackContext.success(); callbackContext.success();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -418,7 +423,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(
this.cordova.getActivity()); cordovaActivity);
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();
@ -433,7 +438,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(
this.cordova.getActivity(), R.layout.test_notification_layout, cordovaActivity, 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";
builder.layoutIconDrawable = R.drawable.jpush_notification_icon; builder.layoutIconDrawable = R.drawable.jpush_notification_icon;
@ -448,7 +453,7 @@ public class JPushPlugin extends CordovaPlugin {
} }
void clearAllNotification(JSONArray data, CallbackContext callbackContext) { void clearAllNotification(JSONArray data, CallbackContext callbackContext) {
JPushInterface.clearAllNotifications(this.cordova.getActivity()); JPushInterface.clearAllNotifications(cordovaActivity);
//callbackContext.success(); //callbackContext.success();
} }
@ -461,8 +466,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(this.cordova.getActivity(), JPushInterface.clearNotificationById(cordovaActivity, notificationId);
notificationId);
} else { } else {
callbackContext.error("error id"); callbackContext.error("error id");
} }
@ -486,18 +490,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(this.cordova.getActivity(), ln); JPushInterface.addLocalNotification(cordovaActivity, 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(this.cordova.getActivity(), JPushInterface.removeLocalNotification(cordovaActivity, notificationID);
notificationID);
} }
void clearLocalNotifications(JSONArray data, CallbackContext callbackContext) { void clearLocalNotifications(JSONArray data, CallbackContext callbackContext) {
JPushInterface.clearLocalNotifications(this.cordova.getActivity()); JPushInterface.clearLocalNotifications(cordovaActivity);
} }
/** /**