2013-10-13 04:38:26 -07:00
|
|
|
package org.thoughtcrime.securesms.service;
|
|
|
|
|
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.os.PowerManager;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-11-27 11:08:58 -08:00
|
|
|
import org.thoughtcrime.securesms.push.PushServiceSocketFactory;
|
2014-02-06 18:06:23 -08:00
|
|
|
import org.thoughtcrime.securesms.util.DirectoryHelper;
|
2013-11-20 00:45:51 -08:00
|
|
|
|
2013-10-13 04:38:26 -07:00
|
|
|
import java.util.concurrent.Executor;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
public class DirectoryRefreshService extends Service {
|
|
|
|
|
|
|
|
public static final String REFRESH_ACTION = "org.whispersystems.whisperpush.REFRESH_ACTION";
|
|
|
|
|
|
|
|
private static final Executor executor = Executors.newSingleThreadExecutor();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand (Intent intent, int flags, int startId) {
|
|
|
|
if (REFRESH_ACTION.equals(intent.getAction())) {
|
|
|
|
handleRefreshAction();
|
|
|
|
}
|
|
|
|
return START_NOT_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleRefreshAction() {
|
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
|
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Directory Refresh");
|
|
|
|
wakeLock.acquire();
|
|
|
|
|
|
|
|
executor.execute(new RefreshRunnable(wakeLock));
|
|
|
|
}
|
|
|
|
|
|
|
|
private class RefreshRunnable implements Runnable {
|
|
|
|
private final PowerManager.WakeLock wakeLock;
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
public RefreshRunnable(PowerManager.WakeLock wakeLock) {
|
|
|
|
this.wakeLock = wakeLock;
|
|
|
|
this.context = DirectoryRefreshService.this.getApplicationContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Log.w("DirectoryRefreshService", "Refreshing directory...");
|
2013-10-18 22:45:27 -07:00
|
|
|
|
2014-02-06 18:06:23 -08:00
|
|
|
DirectoryHelper.refreshDirectory(context);
|
2013-10-13 04:38:26 -07:00
|
|
|
|
|
|
|
Log.w("DirectoryRefreshService", "Directory refresh complete...");
|
|
|
|
} finally {
|
|
|
|
if (wakeLock != null && wakeLock.isHeld())
|
|
|
|
wakeLock.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|