Skip to content

Commit

Permalink
Reformat code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin committed May 3, 2018
1 parent b31ee74 commit 09b41fd
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 153 deletions.
46 changes: 29 additions & 17 deletions src/android/FirebasePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.v4.app.NotificationManagerCompat;
import android.util.Base64;
import android.util.Log;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
Expand All @@ -22,14 +23,17 @@
import com.google.firebase.crash.FirebaseCrash;
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.metrics.Trace;

import me.leolin.shortcutbadger.ShortcutBadger;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaWebView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand All @@ -38,6 +42,7 @@

// Firebase PhoneAuth
import java.util.concurrent.TimeUnit;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.FirebaseException;
Expand Down Expand Up @@ -136,22 +141,25 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
this.activateFetched(callbackContext);
return true;
} else if (action.equals("fetch")) {
if (args.length() > 0)
if (args.length() > 0) {
this.fetch(callbackContext, args.getLong(0));
else
} else {
this.fetch(callbackContext);
}
return true;
} else if (action.equals("getByteArray")) {
if (args.length() > 1)
if (args.length() > 1) {
this.getByteArray(callbackContext, args.getString(0), args.getString(1));
else
} else {
this.getByteArray(callbackContext, args.getString(0), null);
}
return true;
} else if (action.equals("getValue")) {
if (args.length() > 1)
if (args.length() > 1) {
this.getValue(callbackContext, args.getString(0), args.getString(1));
else
} else {
this.getValue(callbackContext, args.getString(0), null);
}
return true;
} else if (action.equals("getInfo")) {
this.getInfo(callbackContext);
Expand All @@ -160,10 +168,11 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
this.setConfigSettings(callbackContext, args.getJSONObject(0));
return true;
} else if (action.equals("setDefaults")) {
if (args.length() > 1)
if (args.length() > 1) {
this.setDefaults(callbackContext, args.getJSONObject(0), args.getString(1));
else
} else {
this.setDefaults(callbackContext, args.getJSONObject(0), null);
}
return true;
} else if (action.equals("verifyPhoneNumber")) {
this.verifyPhoneNumber(callbackContext, args.getString(0), args.getInt(1));
Expand All @@ -181,6 +190,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
this.setAnalyticsCollectionEnabled(callbackContext, args.getBoolean(0));
return true;
}

return false;
}

Expand Down Expand Up @@ -209,12 +219,12 @@ public void onDestroy() {
appView.handleDestroy();
}
}

private void onNotificationOpen(final CallbackContext callbackContext) {
FirebasePlugin.notificationCallbackContext = callbackContext;
if (FirebasePlugin.notificationStack != null) {
for (Bundle bundle : FirebasePlugin.notificationStack) {
FirebasePlugin.sendNotification(bundle,this.cordova.getActivity().getApplicationContext());
FirebasePlugin.sendNotification(bundle, this.cordova.getActivity().getApplicationContext());
}
FirebasePlugin.notificationStack.clear();
}
Expand All @@ -239,7 +249,7 @@ public void run() {
}

public static void sendNotification(Bundle bundle, Context context) {
if(!FirebasePlugin.hasNotificationsCallback()) {
if (!FirebasePlugin.hasNotificationsCallback()) {
String packageName = context.getPackageName();
if (FirebasePlugin.notificationStack == null) {
FirebasePlugin.notificationStack = new ArrayList<Bundle>();
Expand All @@ -251,7 +261,7 @@ public static void sendNotification(Bundle bundle, Context context) {
intent.setComponent(new ComponentName(packageName, packageName + ".MainActivity"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("cdvStartInBackground", true);

context.startActivity(intent);

return;
Expand Down Expand Up @@ -279,6 +289,7 @@ public static void sendToken(String token) {
if (FirebasePlugin.tokenRefreshCallbackContext == null) {
return;
}

final CallbackContext callbackContext = FirebasePlugin.tokenRefreshCallbackContext;
if (callbackContext != null && token != null) {
PluginResult pluginresult = new PluginResult(PluginResult.Status.OK, token);
Expand Down Expand Up @@ -647,7 +658,7 @@ public void run() {
private static Map<String, Object> defaultsToMap(JSONObject object) throws JSONException {
final Map<String, Object> map = new HashMap<String, Object>();

for (Iterator<String> keys = object.keys(); keys.hasNext();) {
for (Iterator<String> keys = object.keys(); keys.hasNext(); ) {
String key = keys.next();
Object value = object.get(key);

Expand Down Expand Up @@ -675,8 +686,11 @@ private static Map<String, Object> defaultsToMap(JSONObject object) throws JSONE

private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;

public void verifyPhoneNumber(final CallbackContext callbackContext, final String number,
final int timeOutDuration) {
public void verifyPhoneNumber(
final CallbackContext callbackContext,
final String number,
final int timeOutDuration
) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
Expand Down Expand Up @@ -807,7 +821,6 @@ public void run() {
} else {
callbackContext.error("Trace not found");
}

} catch (Exception e) {
FirebaseCrash.log(e.getMessage());
e.printStackTrace();
Expand Down Expand Up @@ -835,7 +848,6 @@ public void run() {
} else {
callbackContext.error("Trace not found");
}

} catch (Exception e) {
FirebaseCrash.log(e.getMessage());
e.printStackTrace();
Expand Down
1 change: 0 additions & 1 deletion src/android/FirebasePluginInstanceIDService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;


public class FirebasePluginInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "FirebasePlugin";
Expand Down
4 changes: 3 additions & 1 deletion src/android/FirebasePluginMessageReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.google.firebase.messaging.RemoteMessage;

public abstract class FirebasePluginMessageReceiver {
public FirebasePluginMessageReceiver(){

public FirebasePluginMessageReceiver() {
FirebasePluginMessageReceiverManager.register(this);
}

/**
* Concrete subclasses should override this and return true if they handle the received message.
*
* @param remoteMessage
* @return true if the received message was handled by the receiver so should not be handled by FirebasePlugin.
*/
Expand Down
10 changes: 6 additions & 4 deletions src/android/FirebasePluginMessageReceiverManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
import java.util.List;

public class FirebasePluginMessageReceiverManager {

private static List<FirebasePluginMessageReceiver> receivers = new ArrayList<FirebasePluginMessageReceiver>();

public static void register(FirebasePluginMessageReceiver receiver){
public static void register(FirebasePluginMessageReceiver receiver) {
receivers.add(receiver);
}

public static boolean onMessageReceived(RemoteMessage remoteMessage){
public static boolean onMessageReceived(RemoteMessage remoteMessage) {
boolean handled = false;
for(FirebasePluginMessageReceiver receiver : receivers){
for (FirebasePluginMessageReceiver receiver : receivers) {
boolean wasHandled = receiver.onMessageReceived(remoteMessage);
if(wasHandled){
if (wasHandled) {
handled = true;
}
}

return handled;
}
}
93 changes: 51 additions & 42 deletions src/android/FirebasePluginMessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ public class FirebasePluginMessagingService extends FirebaseMessagingService {

private static final String TAG = "FirebasePlugin";

/**
* Get a string from resources without importing the .R package
* @param name Resource Name
* @return Resource
*/
/**
* Get a string from resources without importing the .R package
*
* @param name Resource Name
* @return Resource
*/
private String getStringResource(String name) {
return this.getString(
this.getResources().getIdentifier(
name, "string", this.getPackageName()));
return this.getString(
this.getResources().getIdentifier(
name, "string", this.getPackageName()
)
);
}

/**
Expand All @@ -55,9 +58,11 @@ public void onMessageReceived(RemoteMessage remoteMessage) {

// Pass the message to the receiver manager so any registered receivers can decide to handle it
boolean wasHandled = FirebasePluginMessageReceiverManager.onMessageReceived(remoteMessage);
if(wasHandled){
if (wasHandled) {
Log.d(TAG, "Message was handled by a registered receiver");
return; // don't process the message in this method

// Don't process the message in this method.
return;
}

// TODO(developer): Handle FCM messages here.
Expand All @@ -68,7 +73,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
String sound = null;
String lights = null;
Map<String, String> data = remoteMessage.getData();

if (remoteMessage.getNotification() != null) {
title = remoteMessage.getNotification().getTitle();
text = remoteMessage.getNotification().getBody();
Expand All @@ -79,12 +84,15 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
id = data.get("id");
sound = data.get("sound");
lights = data.get("lights"); //String containing hex ARGB color, miliseconds on, miliseconds off, example: '#FFFF00FF,1000,3000'
if(TextUtils.isEmpty(text)) text = data.get("body");

if (TextUtils.isEmpty(text)) {
text = data.get("body");
}
}

if(TextUtils.isEmpty(id)){
if (TextUtils.isEmpty(id)) {
Random rand = new Random();
int n = rand.nextInt(50) + 1;
int n = rand.nextInt(50) + 1;
id = Integer.toString(n);
}

Expand All @@ -108,23 +116,26 @@ private void sendNotification(String id, String title, String messageBody, Map<S
for (String key : data.keySet()) {
bundle.putString(key, data.get(key));
}

if (showNotification) {
Intent intent = new Intent(this, OnNotificationOpenReceiver.class);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id.hashCode(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

String channelId = this.getStringResource("default_notification_channel_id");
String channelName = this.getStringResource("default_notification_channel_name");
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
.setContentTitle(title)
.setContentText(messageBody)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
notificationBuilder
.setContentTitle(title)
.setContentText(messageBody)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
;

int resID = getResources().getIdentifier("notification_icon", "drawable", getPackageName());
if (resID != 0) {
Expand All @@ -135,34 +146,34 @@ private void sendNotification(String id, String title, String messageBody, Map<S

if (sound != null) {
Log.d(TAG, "sound before path is: " + sound);
Uri soundPath = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getPackageName() + "/raw/" + sound);
Uri soundPath = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/" + sound);
Log.d(TAG, "Parsed sound is: " + soundPath.toString());
notificationBuilder.setSound(soundPath);
} else {
Log.d(TAG, "Sound was null ");
}

if(lights != null) {
try {
String[] lightsComponents = lights.replaceAll("\\s","").split(",");
if(lightsComponents.length == 3) {
int lightArgb = Color.parseColor(lightsComponents[0]);
int lightOnMs = Integer.parseInt(lightsComponents[1]);
int lightOffMs = Integer.parseInt(lightsComponents[2]);
notificationBuilder.setLights(lightArgb, lightOnMs, lightOffMs);
if (lights != null) {
try {
String[] lightsComponents = lights.replaceAll("\\s", "").split(",");
if (lightsComponents.length == 3) {
int lightArgb = Color.parseColor(lightsComponents[0]);
int lightOnMs = Integer.parseInt(lightsComponents[1]);
int lightOffMs = Integer.parseInt(lightsComponents[2]);

notificationBuilder.setLights(lightArgb, lightOnMs, lightOffMs);
}
} catch (Exception e) {
}
}catch(Exception e){}
}

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int accentID = getResources().getIdentifier("accent", "color", getPackageName());
notificationBuilder.setColor(getResources().getColor(accentID, null));
}

Notification notification = notificationBuilder.build();
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
int iconID = android.R.id.icon;
int notiID = getResources().getIdentifier("notification_big", "drawable", getPackageName());
if (notification.contentView != null) {
Expand All @@ -173,10 +184,8 @@ private void sendNotification(String id, String title, String messageBody, Map<S

// Since android Oreo notification channel is needed.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
channelName,
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}

notificationManager.notify(id.hashCode(), notification);
Expand Down
Loading

0 comments on commit 09b41fd

Please sign in to comment.