2015-04-13 01:01:52 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
|
|
|
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.support.v4.app.NotificationCompat;
|
2015-04-20 22:04:14 +02:00
|
|
|
import android.util.Log;
|
2015-05-01 01:49:43 +02:00
|
|
|
|
2015-04-20 22:39:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver;
|
|
|
|
|
2015-04-13 01:01:52 +02:00
|
|
|
public class GB {
|
|
|
|
public static final int NOTIFICATION_ID = 1;
|
2015-04-20 22:04:14 +02:00
|
|
|
private static final String TAG = "GB";
|
2015-04-13 01:01:52 +02:00
|
|
|
|
|
|
|
public static Notification createNotification(String text, Context context) {
|
|
|
|
Intent notificationIntent = new Intent(context, ControlCenter.class);
|
|
|
|
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|
|
|
|
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
|
|
|
|
notificationIntent, 0);
|
|
|
|
|
|
|
|
return new NotificationCompat.Builder(context)
|
2015-05-01 01:26:12 +02:00
|
|
|
.setContentTitle(context.getString(R.string.gadgetbridge))
|
2015-04-13 01:01:52 +02:00
|
|
|
.setTicker(text)
|
|
|
|
.setContentText(text)
|
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setContentIntent(pendingIntent)
|
|
|
|
.setOngoing(true).build();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void updateNotification(String text, Context context) {
|
|
|
|
Notification notification = createNotification(text, context);
|
|
|
|
|
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.notify(NOTIFICATION_ID, notification);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setReceiversEnableState(boolean enable, Context context) {
|
2015-04-20 22:04:14 +02:00
|
|
|
Log.i(TAG, "Setting broadcast receivers to: " + enable);
|
|
|
|
final Class<?>[] receiverClasses = {
|
2015-04-13 01:01:52 +02:00
|
|
|
PhoneCallReceiver.class,
|
|
|
|
SMSReceiver.class,
|
|
|
|
K9Receiver.class,
|
|
|
|
MusicPlaybackReceiver.class,
|
|
|
|
//NotificationListener.class, // disabling this leads to loss of permission to read notifications
|
|
|
|
};
|
|
|
|
|
|
|
|
int newState;
|
|
|
|
|
|
|
|
if (enable) {
|
|
|
|
newState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
|
|
|
|
} else {
|
|
|
|
newState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
|
2015-04-20 22:04:14 +02:00
|
|
|
for (Class<?> receiverClass : receiverClasses) {
|
2015-04-13 01:01:52 +02:00
|
|
|
ComponentName compName = new ComponentName(context, receiverClass);
|
|
|
|
|
|
|
|
pm.setComponentEnabledSetting(compName, newState, PackageManager.DONT_KILL_APP);
|
|
|
|
}
|
|
|
|
}
|
2015-04-26 00:53:48 +02:00
|
|
|
|
|
|
|
public static String hexdump(byte[] buffer, int offset, int length) {
|
2015-04-25 23:13:22 +02:00
|
|
|
final char[] hexArray = "0123456789ABCDEF".toCharArray();
|
|
|
|
char[] hexChars = new char[length * 2];
|
2015-04-26 00:53:48 +02:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
int v = buffer[i + offset] & 0xFF;
|
|
|
|
hexChars[i * 2] = hexArray[v >>> 4];
|
|
|
|
hexChars[i * 2 + 1] = hexArray[v & 0x0F];
|
2015-04-25 23:13:22 +02:00
|
|
|
}
|
|
|
|
return new String(hexChars);
|
|
|
|
}
|
2015-04-13 01:01:52 +02:00
|
|
|
}
|