Files
FairEmail/app/src/main/java/eu/faircode/email/ServiceTileUnseen.java

98 lines
2.9 KiB
Java
Raw Normal View History

2018-11-07 08:09:05 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.util.Log;
import java.util.List;
import androidx.lifecycle.LifecycleService;
import androidx.lifecycle.Observer;
@TargetApi(Build.VERSION_CODES.N)
2018-11-07 12:10:58 +00:00
public class ServiceTileUnseen extends TileService {
2018-11-07 08:09:05 +00:00
LifecycleService owner = new LifecycleService();
@Override
public void onCreate() {
owner.onCreate();
super.onCreate();
}
@Override
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() {
2018-11-07 12:10:58 +00:00
Log.i(Helper.TAG, "Start tile unseen");
2018-11-07 08:09:05 +00:00
DB db = DB.getInstance(this);
db.message().liveUnseenUnified().observe(owner, new Observer<List<TupleMessageEx>>() {
@Override
public void onChanged(List<TupleMessageEx> messages) {
2018-11-07 12:10:58 +00:00
Log.i(Helper.TAG, "Update tile unseen=" + messages.size());
2018-11-07 08:09:05 +00:00
Tile tile = getQsTile();
if (tile != null) {
tile.setState(messages.size() > 0 ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
tile.setLabel(getResources().getQuantityString(
R.plurals.title_tile_unseen, messages.size(), messages.size()));
tile.updateTile();
}
}
});
}
public void onStopListening() {
2018-11-07 12:10:58 +00:00
Log.i(Helper.TAG, "Stop tile unseen");
2018-11-07 08:09:05 +00:00
DB db = DB.getInstance(this);
db.message().liveUnseenUnified().removeObservers(owner);
}
public void onClick() {
2018-11-07 12:10:58 +00:00
Log.i(Helper.TAG, "Click tile unseen");
2018-11-07 08:09:05 +00:00
Intent clear = new Intent(this, ServiceSynchronize.class);
clear.setAction("clear");
startService(clear);
}
}