Android Open Source - growthpush-android Base Receive Handler






From Project

Back to project page growthpush-android.

License

The source code is released under:

Apache License

If you think the Android project growthpush-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.growthpush.handler;
/* w w  w  .j a  v  a  2 s  .c o m*/
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.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.growthpush.utils.PermissionUtils;
import com.growthpush.view.AlertActivity;

public class BaseReceiveHandler implements ReceiveHandler {

  private Callback callback = new Callback();

  public BaseReceiveHandler() {
    super();
  }

  public BaseReceiveHandler(BaseReceiveHandler.Callback callback) {
    this();
    setCallback(callback);
  }

  @Override
  public void onReceive(Context context, Intent intent) {
  }

  protected void showAlert(Context context, Intent intent) {

    if (context == null || intent == null || intent.getExtras() == null)
      return;

    Intent alertIntent = new Intent(context, AlertActivity.class);
    alertIntent.putExtras(intent.getExtras());
    alertIntent.putExtra("showDialog", true);
    alertIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_NEW_TASK);

    AlertActivity.setSharedCallback(callback);
    context.startActivity(alertIntent);

  }

  protected void addNotification(Context context, Intent intent) {

    if (context == null || intent == null || intent.getExtras() == null)
      return;

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify("GrowthPush" + context.getPackageName(), 1, generateNotification(context, intent.getExtras()));

  }

  private Notification generateNotification(Context context, Bundle extras) {
    PackageManager packageManager = context.getPackageManager();

    int icon = 0;
    String title = "";
    try {
      ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
      icon = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0).icon;
      title = packageManager.getApplicationLabel(applicationInfo).toString();
    } catch (NameNotFoundException e) {
    }

    String message = extras.getString("message");
    boolean sound = false;
    if (extras.containsKey("sound"))
      sound = Boolean.valueOf(extras.getString("sound"));

    Intent intent = new Intent(context, AlertActivity.class);
    intent.putExtras(extras);
    intent.putExtra("showDialog", false);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    AlertActivity.setSharedCallback(callback);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setTicker(title);
    builder.setSmallIcon(icon);
    builder.setContentTitle(title);
    builder.setContentText(message);
    builder.setContentIntent(pendingIntent);
    builder.setWhen(System.currentTimeMillis());
    builder.setAutoCancel(true);

    if (sound && PermissionUtils.permitted(context, "android.permission.VIBRATE"))
      builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);

    return builder.build();

  }

  public Callback getCallback() {
    return callback;
  }

  public void setCallback(Callback callback) {
    this.callback = callback;
  }

  public static class Callback {

    public void onOpen(Context context, Intent intent) {
      context.startActivity(context.getPackageManager().getLaunchIntentForPackage(context.getPackageName())
          .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }

  }

}




Java Source Code List

com.growthpush.BroadcastReceiver.java
com.growthpush.GrowthPushException.java
com.growthpush.GrowthPush.java
com.growthpush.Logger.java
com.growthpush.Preference.java
com.growthpush.Thread.java
com.growthpush.bridge.ExternalFrameworkBridge.java
com.growthpush.bridge.ExternalFrameworkBroadcastReceiver.java
com.growthpush.growthpushsample.MainActivity.java
com.growthpush.handler.BaseReceiveHandler.java
com.growthpush.handler.DefaultReceiveHandler.java
com.growthpush.handler.OnlyAlertReceiveHandler.java
com.growthpush.handler.OnlyNotificationReceiveHandler.java
com.growthpush.handler.ReceiveHandler.java
com.growthpush.model.ClientStatus.java
com.growthpush.model.Client.java
com.growthpush.model.Environment.java
com.growthpush.model.Error.java
com.growthpush.model.Event.java
com.growthpush.model.Model.java
com.growthpush.model.Tag.java
com.growthpush.utils.DeviceUtils.java
com.growthpush.utils.IOUtils.java
com.growthpush.utils.PermissionUtils.java
com.growthpush.utils.SystemUtils.java
com.growthpush.view.AlertActivity.java
com.growthpush.view.AlertFragment.java
com.growthpush.view.DialogCallback.java