Last active
August 29, 2015 14:20
-
-
Save SemonCat/82b480f2d2403a31ed52 to your computer and use it in GitHub Desktop.
zParsePushBroadcastReceiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.asus.push.receiver; | |
import android.app.AlarmManager; | |
import android.app.Notification; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.pm.ApplicationInfo; | |
import android.content.pm.PackageInfo; | |
import android.content.pm.PackageManager; | |
import android.content.pm.ResolveInfo; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.net.Uri; | |
import android.os.AsyncTask; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.provider.Settings; | |
import android.support.v4.app.NotificationCompat; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.View; | |
import com.asus.push.analytics.zAnalytics; | |
import com.asus.push.bean.zIntent; | |
import com.asus.push.service.ProxyService; | |
import com.asus.push.util.SharedPreferencesUtil; | |
import com.asus.push.zPush; | |
import com.nostra13.universalimageloader.core.ImageLoader; | |
import com.nostra13.universalimageloader.core.assist.FailReason; | |
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener; | |
import com.parse.ParseInstallation; | |
import com.parse.ParsePushBroadcastReceiver; | |
import com.parse.PushService; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.Calendar; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Random; | |
/** | |
* Created by SemonCat on 15/1/30. | |
*/ | |
public abstract class zParsePushBroadcastReceiver extends ParsePushBroadcastReceiver { | |
private static final String TAG = zParsePushBroadcastReceiver.class.getName(); | |
private static final String ACTION_SCHEDULE_UPDATE = "com.asus.push.action.SCHEDULE_UPDATE"; | |
public static final String KEY_PROMOTE = "promote"; | |
public static final String KEY_PROMOTE_URL = "promote_url"; | |
public static final String KEY_TITLE = "title"; | |
public static final String KEY_MSG = "message"; | |
public static final String KEY_ACTION_INTENT_JSON = "action_intent_json"; | |
public static final String KEY_MESSAGE_ID = "message_id"; | |
public static final String KEY_LARGE_ICON = "largeIcon"; | |
public static final String KEY_SMALL_ICON = "smallIcon"; | |
public static final String KEY_BIG_PICTURE = "bigPicture"; | |
private static final int RANDOM_TIME_MAX = 1000 * 60 * 60 * 24 * 7; | |
public abstract int getSmallIcon(); | |
public abstract int getLargeIcon(); | |
public void customNotification(Notification.Builder builder) { | |
} | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
switch (intent.getAction()) { | |
case Intent.ACTION_BOOT_COMPLETED: | |
Log.d(TAG, "ACTION_BOOT_COMPLETED"); | |
if (getDeviceProvisioned(context)==1){ | |
zPush.getInstance(context).Register(); | |
} | |
break; | |
case Intent.ACTION_LOCALE_CHANGED: | |
Log.d(TAG, "ACTION_LOCALE_CHANGED"); | |
zPush.getInstance(context).Register(); | |
break; | |
case Intent.ACTION_TIMEZONE_CHANGED: | |
Log.d(TAG, "ACTION_TIMEZONE_CHANGED"); | |
zPush.getInstance(context).Register(); | |
break; | |
case Intent.ACTION_MY_PACKAGE_REPLACED: | |
Log.d(TAG, "ACTION_MY_PACKAGE_REPLACED"); | |
String deviceModelLowerCase = Build.MODEL.toLowerCase(); | |
if (deviceModelLowerCase.contains("z00d") || | |
deviceModelLowerCase.contains("z008") || | |
deviceModelLowerCase.contains("z00a")){ | |
Log.d(TAG,"App Update Register"); | |
zPush.getInstance(context).Register(); | |
} | |
break; | |
case ACTION_PUSH_DELETE: | |
zAnalytics.Dismiss(context,intent.getStringExtra(KEY_MESSAGE_ID)); | |
break; | |
} | |
super.onReceive(context, intent); | |
} | |
@Override | |
protected void onPushReceive(Context context, Intent intent) { | |
String data = intent.getExtras().getString(KEY_PUSH_DATA); | |
Log.d(TAG, "Receive:" + data); | |
Bundle dataBundle = getParseJsonToBundle(data); | |
makeNotification(context, intent, dataBundle); | |
} | |
private void makeNotification(Context context, Intent intent, Bundle extras) { | |
if (!extras.containsKey(KEY_ACTION_INTENT_JSON)) return; | |
String messageId = extras.getString(KEY_MESSAGE_ID, ""); | |
zAnalytics.Receive(context, messageId); | |
String actionIntentJson = extras.getString(KEY_ACTION_INTENT_JSON); | |
String type = extras.getString(ProxyService.KEY_ACTION_TYPE, ProxyService.TYPE_ACTIVITY); | |
Intent action = getIntentByJson(context, actionIntentJson); | |
boolean pushEnable = SharedPreferencesUtil.isPushEnable(context); | |
if (!pushEnable) { | |
zAnalytics.Disable(context, messageId); | |
return; | |
} | |
if (action != null) { | |
PackageManager pm = context.getPackageManager(); | |
List<ResolveInfo> list = null; | |
try { | |
if (TextUtils.equals(type, ProxyService.TYPE_SERVICE)) { | |
list = pm.queryIntentServices(action, PackageManager.GET_SERVICES); | |
} else if (TextUtils.equals(type, ProxyService.TYPE_BROADCAST)) { | |
list = pm.queryBroadcastReceivers(action, PackageManager.GET_RECEIVERS); | |
} else { | |
list = pm.queryIntentActivities(action, PackageManager.GET_ACTIVITIES); | |
if (list == null || list.size() <= 0) { | |
boolean promote = Boolean.parseBoolean((action.getExtras().getString(KEY_PROMOTE, "false"))); | |
String promote_url = action.getExtras().getString(KEY_PROMOTE_URL, ""); | |
String packageName = action.getPackage(); | |
if (promote && !TextUtils.isEmpty(packageName)) { | |
Uri uri; | |
if (TextUtils.isEmpty(promote_url)) { | |
uri = Uri.parse("market://details?id=" + packageName); | |
} else { | |
uri = Uri.parse(promote_url); | |
} | |
action = new Intent(Intent.ACTION_VIEW); | |
action.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
action.setData(uri); | |
list = pm.queryIntentActivities(action, PackageManager.GET_ACTIVITIES); | |
} | |
} | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
if (list != null && list.size() > 0) { | |
zAnalytics.Show(context, messageId); | |
final int notifyID = 0x001; | |
Random random = new Random(); | |
int contentIntentRequestCode = random.nextInt(); | |
int deleteIntentRequestCode = random.nextInt(); | |
Intent proxy = new Intent(); | |
proxy.setClass(context, ProxyService.class); | |
proxy.putExtra(ProxyService.KEY_ACTION_INTENT, action); | |
proxy.putExtra(ProxyService.KEY_ACTION_TYPE, type); | |
proxy.putExtra(KEY_MESSAGE_ID, messageId); | |
proxy.putExtra(ProxyService.KEY_PARSE_INTENT, intent); | |
int flags = PendingIntent.FLAG_CANCEL_CURRENT; | |
PendingIntent pendingIntent = PendingIntent.getService(context, contentIntentRequestCode, proxy, flags); | |
Intent deleteIntent = new Intent("com.parse.push.intent.DELETE"); | |
deleteIntent.putExtra(KEY_MESSAGE_ID, messageId); | |
deleteIntent.setPackage(context.getPackageName()); | |
PendingIntent pDeleteIntent = PendingIntent.getBroadcast(context, deleteIntentRequestCode, deleteIntent, flags); | |
final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | |
String title = extras.getString(KEY_TITLE, getAppName(context)); | |
String msg = extras.getString(KEY_MSG, "Message"); | |
String largeIconUrl = extras.getString(KEY_LARGE_ICON); | |
String smallIcon = extras.getString(KEY_SMALL_ICON); | |
String bigPictureUrl = extras.getString(KEY_BIG_PICTURE); | |
final Notification.Builder builder = new Notification.Builder(context); | |
builder | |
.setSmallIcon(getSmallIcon()) | |
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), getLargeIcon())) | |
.setContentTitle(title) | |
.setContentText(msg) | |
.setContentIntent(pendingIntent) | |
.setDeleteIntent(pDeleteIntent) | |
.setAutoCancel(true) | |
.setTicker(msg) | |
.setPriority(Notification.PRIORITY_HIGH) | |
.setVibrate(new long[0]) | |
.setStyle(new Notification.BigTextStyle().bigText(msg)); | |
if (!TextUtils.isEmpty(smallIcon)) { | |
int smallDrawableResourceId = context.getResources().getIdentifier(smallIcon, "drawable", context.getPackageName()); | |
int smallMipmapResourceId = context.getResources().getIdentifier(smallIcon, "mipmap", context.getPackageName()); | |
int smallResourceId = smallDrawableResourceId == 0 ? smallMipmapResourceId : smallDrawableResourceId; | |
if (smallResourceId != 0) { | |
builder.setSmallIcon(smallResourceId); | |
} | |
} | |
customNotification(builder); | |
decodeImage(context, largeIconUrl, bigPictureUrl, new OnDecodeCallback() { | |
@Override | |
public void OnDecodeLargeIconDone(Bitmap largeIcon) { | |
builder.setLargeIcon(largeIcon); | |
} | |
@Override | |
public void OnDecodeBigPictureDone(Bitmap bigPicture) { | |
builder.setStyle(new Notification.BigPictureStyle().bigPicture(bigPicture)); | |
} | |
@Override | |
public void OnDecodeDone() { | |
Notification notification = builder.build(); | |
notificationManager.notify(notifyID, notification); // 發送通知 | |
} | |
}); | |
} | |
} | |
} | |
private String getAppName(Context context) { | |
final PackageManager pm = context.getApplicationContext().getPackageManager(); | |
ApplicationInfo ai; | |
try { | |
ai = pm.getApplicationInfo(context.getPackageName(), 0); | |
} catch (final PackageManager.NameNotFoundException e) { | |
ai = null; | |
} | |
return (String) (ai != null ? pm.getApplicationLabel(ai) : "Title"); | |
} | |
private Intent getIntentByJson(Context context, String json) { | |
return zIntent.toIntent(context, json); | |
} | |
private Bundle getParseJsonToBundle(String json) { | |
Bundle bundle = new Bundle(); | |
try { | |
JSONObject jsonObject = new JSONObject(json); | |
Iterator<String> keys = jsonObject.keys(); | |
while (keys.hasNext()) { | |
String key = keys.next(); | |
bundle.putString(key, jsonObject.getString(key)); | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return bundle; | |
} | |
private void scheduleUpdate(Context context) { | |
Random random = new Random(getVersion(context)); | |
long execTime = random.nextInt(RANDOM_TIME_MAX); | |
Intent intent = new Intent(context, zParsePushBroadcastReceiver.class); | |
intent.setAction(ACTION_SCHEDULE_UPDATE); | |
PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT); | |
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | |
am.set(AlarmManager.RTC_WAKEUP, execTime, pi); | |
} | |
private int getVersion(Context context) { | |
try { | |
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_META_DATA); | |
return pInfo.versionCode; | |
} catch (PackageManager.NameNotFoundException e) { | |
return 0; | |
} | |
} | |
private interface OnDecodeCallback{ | |
void OnDecodeLargeIconDone(Bitmap largeIcon); | |
void OnDecodeBigPictureDone(Bitmap bigPicture); | |
void OnDecodeDone(); | |
} | |
private void decodeImage(final Context context,final String largeIcon,final String bigPicture,final OnDecodeCallback callback){ | |
if (callback!=null){ | |
final Handler handler = new Handler(); | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
Bitmap largeIconBitmap = ImageLoader.getInstance().loadImageSync(largeIcon); | |
if (largeIcon!=null){ | |
callback.OnDecodeLargeIconDone(largeIconBitmap); | |
} | |
Bitmap bigPictureBitmap = ImageLoader.getInstance().loadImageSync(bigPicture); | |
if (bigPicture!=null){ | |
callback.OnDecodeBigPictureDone(bigPictureBitmap); | |
} | |
handler.post(new Runnable() { | |
@Override | |
public void run() { | |
callback.OnDecodeDone(); | |
} | |
}); | |
} | |
}).start(); | |
} | |
} | |
private int getDeviceProvisioned(Context context){ | |
try { | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1){ | |
return Settings.System.getInt(context.getContentResolver(), Settings.System.DEVICE_PROVISIONED); | |
}else{ | |
return Settings.Global.getInt(context.getContentResolver(), Settings.Global.DEVICE_PROVISIONED); | |
} | |
} catch (Settings.SettingNotFoundException e) { | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment