2015-04-13 01:01:52 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
2015-05-07 22:15:53 +02:00
|
|
|
import android.bluetooth.BluetoothAdapter;
|
2015-04-13 01:01:52 +02:00
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
2015-05-12 06:28:11 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-05-01 01:49:43 +02:00
|
|
|
|
2015-06-25 23:34:50 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.ByteOrder;
|
|
|
|
|
2015-06-24 23:55:51 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventScreenshot;
|
2015-04-20 22:39:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver;
|
2015-05-04 01:03:56 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver;
|
2015-04-20 22:39:35 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver;
|
2015-05-07 23:46:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.externalevents.TimeChangeReceiver;
|
2015-04-20 22:39:35 +02:00
|
|
|
|
2015-04-13 01:01:52 +02:00
|
|
|
public class GB {
|
|
|
|
public static final int NOTIFICATION_ID = 1;
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(GB.class);
|
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 10:08:24 +02:00
|
|
|
.setContentTitle(context.getString(R.string.app_name))
|
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-05-12 06:28:11 +02:00
|
|
|
LOG.info("Setting broadcast receivers to: " + enable);
|
2015-04-20 22:04:14 +02:00
|
|
|
final Class<?>[] receiverClasses = {
|
2015-04-13 01:01:52 +02:00
|
|
|
PhoneCallReceiver.class,
|
|
|
|
SMSReceiver.class,
|
|
|
|
K9Receiver.class,
|
2015-05-04 01:03:56 +02:00
|
|
|
PebbleReceiver.class,
|
2015-04-13 01:01:52 +02:00
|
|
|
MusicPlaybackReceiver.class,
|
2015-05-07 23:46:18 +02:00
|
|
|
TimeChangeReceiver.class,
|
2015-04-13 01:01:52 +02:00
|
|
|
//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
|
|
|
|
2015-05-07 22:15:53 +02:00
|
|
|
static boolean isBluetoothEnabled() {
|
|
|
|
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
|
return adapter != null && adapter.isEnabled();
|
|
|
|
}
|
|
|
|
|
2015-05-09 23:54:47 +02:00
|
|
|
public static boolean supportsBluetoothLE() {
|
|
|
|
return GBApplication.getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE);
|
|
|
|
}
|
|
|
|
|
2015-04-26 00:53:48 +02:00
|
|
|
public static String hexdump(byte[] buffer, int offset, int length) {
|
2015-06-24 23:55:51 +02:00
|
|
|
if (length == -1) {
|
2015-06-16 23:14:51 +02:00
|
|
|
length = buffer.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-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
public static String formatRssi(short rssi) {
|
|
|
|
return String.valueOf(rssi);
|
|
|
|
}
|
2015-06-24 23:55:51 +02:00
|
|
|
|
2015-06-26 23:17:31 +02:00
|
|
|
public static boolean writeScreenshot(GBDeviceEventScreenshot screenshot, String filename) {
|
2015-06-25 23:34:50 +02:00
|
|
|
|
|
|
|
LOG.info("Will write screenshot: " + screenshot.width + "x" + screenshot.height + "x" + screenshot.bpp + "bpp");
|
|
|
|
final int FILE_HEADER_SIZE = 14;
|
|
|
|
final int INFO_HEADER_SIZE = 40;
|
|
|
|
|
|
|
|
File dir = GBApplication.getContext().getExternalFilesDir(null);
|
|
|
|
if (dir != null) {
|
|
|
|
if (!dir.exists()) {
|
|
|
|
dir.mkdirs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
FileOutputStream fos;
|
|
|
|
try {
|
|
|
|
fos = new FileOutputStream(dir + "/" + filename);
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
2015-06-26 23:17:31 +02:00
|
|
|
return false;
|
2015-06-25 23:34:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer headerbuf = ByteBuffer.allocate(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length);
|
|
|
|
headerbuf.order(ByteOrder.LITTLE_ENDIAN);
|
|
|
|
|
|
|
|
// file header
|
|
|
|
headerbuf.put((byte) 'B');
|
|
|
|
headerbuf.put((byte) 'M');
|
|
|
|
headerbuf.putInt(0); // size in bytes (unconpressed = 0)
|
|
|
|
headerbuf.putInt(0); // reserved
|
|
|
|
headerbuf.putInt(FILE_HEADER_SIZE + INFO_HEADER_SIZE + screenshot.clut.length);
|
|
|
|
|
|
|
|
// info header
|
|
|
|
headerbuf.putInt(INFO_HEADER_SIZE);
|
|
|
|
headerbuf.putInt(screenshot.width);
|
|
|
|
headerbuf.putInt(-screenshot.height);
|
|
|
|
headerbuf.putShort((short) 1); // planes
|
|
|
|
headerbuf.putShort((short) 1); // bit count
|
|
|
|
headerbuf.putInt(0); // compression
|
|
|
|
headerbuf.putInt(0); // length of pixeldata in byte (uncompressed=0)
|
|
|
|
headerbuf.putInt(0); // pixels per meter (x)
|
|
|
|
headerbuf.putInt(0); // pixels per meter (y)
|
|
|
|
headerbuf.putInt(2); // number of colors in CLUT
|
|
|
|
headerbuf.putInt(2); // numbers of used colors
|
|
|
|
headerbuf.put(screenshot.clut);
|
|
|
|
try {
|
|
|
|
fos.write(headerbuf.array());
|
|
|
|
int rowbytes = screenshot.width / 8;
|
|
|
|
byte[] pad = new byte[rowbytes % 4];
|
|
|
|
for (int i = 0; i < screenshot.height; i++) {
|
|
|
|
fos.write(screenshot.data, rowbytes * i, rowbytes);
|
|
|
|
fos.write(pad);
|
|
|
|
}
|
|
|
|
fos.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2015-06-26 23:17:31 +02:00
|
|
|
return false;
|
2015-06-25 23:34:50 +02:00
|
|
|
}
|
2015-06-26 23:17:31 +02:00
|
|
|
return true;
|
2015-06-24 23:55:51 +02:00
|
|
|
}
|
2015-04-13 01:01:52 +02:00
|
|
|
}
|