Refactored tile unseen

This commit is contained in:
M66B 2019-03-06 16:51:44 +00:00
parent 80c28636e6
commit a996601e27
1 changed files with 24 additions and 33 deletions

View File

@ -29,43 +29,17 @@ import android.service.quicksettings.TileService;
import java.util.List; import java.util.List;
import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
@TargetApi(Build.VERSION_CODES.N) @TargetApi(Build.VERSION_CODES.N)
public class ServiceTileUnseen extends TileService { public class ServiceTileUnseen extends TileService {
private LifecycleService owner = new LifecycleService(); private TwoStateOwner owner = new TwoStateOwner();
private LiveData<List<TupleMessageEx>> liveMessages;
@Override @Override
public void onCreate() { public void onCreate() {
owner.onCreate();
super.onCreate(); super.onCreate();
}
@Override DB.getInstance(this).message().liveUnseenNotify().observe(owner, new Observer<List<TupleMessageEx>>() {
public IBinder onBind(Intent intent) {
owner.onBind(intent);
return super.onBind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
owner.onStartCommand(intent, flags, startId);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
owner.onDestroy();
super.onDestroy();
}
public void onStartListening() {
Log.i("Start tile unseen");
liveMessages = DB.getInstance(this).message().liveUnseenNotify();
liveMessages.observe(owner, new Observer<List<TupleMessageEx>>() {
@Override @Override
public void onChanged(List<TupleMessageEx> messages) { public void onChanged(List<TupleMessageEx> messages) {
Log.i("Update tile unseen=" + messages.size()); Log.i("Update tile unseen=" + messages.size());
@ -83,18 +57,35 @@ public class ServiceTileUnseen extends TileService {
}); });
} }
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void onStartListening() {
Log.i("Start tile unseen");
owner.start();
}
public void onStopListening() { public void onStopListening() {
Log.i("Stop tile unseen"); Log.i("Stop tile unseen");
if (liveMessages != null) { owner.stop();
liveMessages.removeObservers(owner);
liveMessages = null;
}
} }
public void onClick() { public void onClick() {
Log.i("Click tile unseen"); Log.i("Click tile unseen");
Intent clear = new Intent(this, ServiceSynchronize.class); Intent clear = new Intent(this, ServiceUI.class);
clear.setAction("clear"); clear.setAction("clear");
startService(clear); startService(clear);
} }