/*
* Copyright 2014, Leanplum, Inc. All rights reserved.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.leanplum;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import com.leanplum.callbacks.VariablesChangedCallback;
import com.leanplum.internal.ActionManager;
import com.leanplum.internal.Constants;
import com.leanplum.internal.Constants.Keys;
import com.leanplum.internal.Constants.Methods;
import com.leanplum.internal.Constants.Params;
import com.leanplum.internal.JsonConverter;
import com.leanplum.internal.LeanplumInternal;
import com.leanplum.internal.Log;
import com.leanplum.internal.Request;
import com.leanplum.internal.Util;
import com.leanplum.internal.VarCache;
import com.leanplum.utils.BitmapUtil;
import com.leanplum.utils.SharedPreferencesUtil;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* Leanplum push notification service class, handling initialization, opening, showing, integration
* verification and registration for push notifications.
*
* @author Andrew First, Anna Orlova
*/
public class LeanplumPushService {
/**
* Leanplum's built-in Google Cloud Messaging sender ID.
*/
public static final String LEANPLUM_SENDER_ID = "44059457771";
private static final String LEANPLUM_PUSH_FCM_LISTENER_SERVICE_CLASS =
"com.leanplum.LeanplumPushFcmListenerService";
private static final String PUSH_FIREBASE_MESSAGING_SERVICE_CLASS =
"com.leanplum.LeanplumPushFirebaseMessagingService";
private static final String LEANPLUM_PUSH_INSTANCE_ID_SERVICE_CLASS =
"com.leanplum.LeanplumPushInstanceIDService";
private static final String LEANPLUM_PUSH_LISTENER_SERVICE_CLASS =
"com.leanplum.LeanplumPushListenerService";
private static final String GCM_RECEIVER_CLASS = "com.google.android.gms.gcm.GcmReceiver";
private static Class extends Activity> callbackClass;
private static LeanplumCloudMessagingProvider provider;
private static boolean isFirebaseEnabled = false;
private static final int NOTIFICATION_ID = 1;
private static final String OPEN_URL = "Open URL";
private static final String URL = "URL";
private static final String OPEN_ACTION = "Open";
private static LeanplumPushNotificationCustomizer customizer;
/**
* Use Firebase Cloud Messaging, instead of the default Google Cloud Messaging.
*/
public static void enableFirebase() {
LeanplumPushService.isFirebaseEnabled = true;
}
/**
* Whether Firebase Cloud Messaging is enabled or not.
*
* @return Boolean - true if enabled
*/
static boolean isFirebaseEnabled() {
return isFirebaseEnabled;
}
/**
* Get Cloud Messaging provider. By default - GCM.
*
* @return LeanplumCloudMessagingProvider - current provider
*/
static LeanplumCloudMessagingProvider getCloudMessagingProvider() {
return provider;
}
/**
* Changes the default activity to launch if the user opens a push notification.
*
* @param callbackClass The activity class.
*/
public static void setDefaultCallbackClass(Class extends Activity> callbackClass) {
LeanplumPushService.callbackClass = callbackClass;
}
/**
* Sets an object used to customize the appearance of notifications.
Call this from your
* Application class's onCreate method so that the customizer is set when your application starts
* in the background.
*/
public static void setCustomizer(LeanplumPushNotificationCustomizer customizer) {
LeanplumPushService.customizer = customizer;
}
/**
* Sets the Google Cloud Messaging/Firebase Cloud Messaging sender ID. Required for push
* notifications to work.
*
* @param senderId The GCM/FCM sender ID to permit notifications from. Use {@link
* LeanplumPushService#LEANPLUM_SENDER_ID} to use the built-in sender ID for GCM. If you have
* multiple sender IDs, use {@link LeanplumPushService#setGcmSenderIds}.
*/
public static void setGcmSenderId(String senderId) {
LeanplumGcmProvider.setSenderId(senderId);
}
/**
* Sets the Google Cloud Messaging/Firebase Cloud Messaging sender ID. Required for push
* notifications to work.
*
* @param senderIds The GCM/FCM sender IDs to permit notifications from. Use {@link
* LeanplumPushService#LEANPLUM_SENDER_ID} to use the built-in sender ID.
*/
public static void setGcmSenderIds(String... senderIds) {
StringBuilder joinedSenderIds = new StringBuilder();
for (String senderId : senderIds) {
if (joinedSenderIds.length() > 0) {
joinedSenderIds.append(',');
}
joinedSenderIds.append(senderId);
}
LeanplumGcmProvider.setSenderId(joinedSenderIds.toString());
}
private static Class extends Activity> getCallbackClass() {
return callbackClass;
}
private static boolean areActionsEmbedded(final Bundle message) {
return message.containsKey(Keys.PUSH_MESSAGE_ACTION);
}
private static void requireMessageContent(
final String messageId, final VariablesChangedCallback onComplete) {
Leanplum.addOnceVariablesChangedAndNoDownloadsPendingHandler(new VariablesChangedCallback() {
@Override
public void variablesChanged() {
try {
Map messages = VarCache.messages();
if (messageId == null || (messages != null && messages.containsKey(messageId))) {
onComplete.variablesChanged();
} else {
// Try downloading the messages again if it doesn't exist.
// Maybe the message was created while the app was running.
Map params = new HashMap<>();
params.put(Params.INCLUDE_DEFAULTS, Boolean.toString(false));
params.put(Params.INCLUDE_MESSAGE_ID, messageId);
Request req = Request.post(Methods.GET_VARS, params);
req.onResponse(new Request.ResponseCallback() {
@Override
public void response(JSONObject response) {
try {
JSONObject getVariablesResponse = Request.getLastResponse(response);
if (getVariablesResponse == null) {
Log.e("No response received from the server. Please contact us to " +
"investigate.");
} else {
Map values = JsonConverter.mapFromJson(
getVariablesResponse.optJSONObject(Constants.Keys.VARS));
Map messages = JsonConverter.mapFromJson(
getVariablesResponse.optJSONObject(Constants.Keys.MESSAGES));
Map regions = JsonConverter.mapFromJson(
getVariablesResponse.optJSONObject(Constants.Keys.REGIONS));
List