|
|
|
@ -18,17 +18,17 @@ import android.os.Looper; |
|
|
|
|
public class PinnedService extends Service { |
|
|
|
|
private static final long FOREGROUND_INTERVAL = 10000L; |
|
|
|
|
private static final long BACKGROUND_INTERVAL = 60000L; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static PinnedService instance; |
|
|
|
|
private static boolean activityInForeground = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Thread loadThread; |
|
|
|
|
private boolean running = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public PinnedService() { |
|
|
|
|
instance = this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the PinnedService instance |
|
|
|
|
* @return the instance or null |
|
|
|
@ -36,31 +36,31 @@ public class PinnedService extends Service { |
|
|
|
|
public static PinnedService getInstance() { |
|
|
|
|
return instance; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void onActivityStart() { |
|
|
|
|
Logger.test("onActivityStart"); |
|
|
|
|
activityInForeground = true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void onActivityStop() { |
|
|
|
|
Logger.test("onActivityStop"); |
|
|
|
|
activityInForeground = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onCreate() { |
|
|
|
|
super.onCreate(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
start(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onDestroy() { |
|
|
|
|
super.onDestroy(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
running = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void start() { |
|
|
|
|
loadThread = new Thread(new Runnable() { |
|
|
|
|
@Override |
|
|
|
@ -70,12 +70,12 @@ public class PinnedService extends Service { |
|
|
|
|
} catch (InterruptedException e1) { |
|
|
|
|
e1.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (running) { |
|
|
|
|
doUpdates(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long timeout = activityInForeground ? FOREGROUND_INTERVAL : BACKGROUND_INTERVAL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
Thread.sleep(timeout); |
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
@ -84,17 +84,17 @@ public class PinnedService extends Service { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
loadThread.start(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void doUpdates() { |
|
|
|
|
List<Pin> pins = PinnedManager.getInstance().getPins(); |
|
|
|
|
for (Pin pin : pins) { |
|
|
|
|
pin.updateWatch(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void callOnPinsChanged() { |
|
|
|
|
new Handler(Looper.getMainLooper()).post(new Runnable() { |
|
|
|
|
@Override |
|
|
|
@ -103,20 +103,20 @@ public class PinnedService extends Service { |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
|
private void showNotification(String text) { |
|
|
|
|
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Notification.Builder builder = new Notification.Builder(this); |
|
|
|
|
builder.setTicker(text); |
|
|
|
|
builder.setContentTitle(text); |
|
|
|
|
builder.setContentText(text); |
|
|
|
|
builder.setSmallIcon(R.drawable.ic_stat_notify); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nm.notify(1, builder.getNotification()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public IBinder onBind(Intent intent) { |
|
|
|
|
return null; |
|
|
|
|