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

101 lines
3.2 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/>.
2020-01-05 18:32:53 +01:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2018-11-07 08:09:05 +00:00
*/
import android.annotation.TargetApi;
import android.content.Intent;
2018-11-09 16:05:10 +00:00
import android.graphics.drawable.Icon;
2018-11-07 08:09:05 +00:00
import android.os.Build;
import android.os.IBinder;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import androidx.lifecycle.Observer;
2019-07-06 10:16:42 +02:00
import java.util.ArrayList;
import java.util.List;
2018-11-07 08:09:05 +00:00
@TargetApi(Build.VERSION_CODES.N)
2018-11-07 12:10:58 +00:00
public class ServiceTileUnseen extends TileService {
2019-03-27 13:24:58 +00:00
private TwoStateOwner owner = new TwoStateOwner("ServiceTileUnseen");
2018-11-07 08:09:05 +00:00
@Override
public void onCreate() {
super.onCreate();
2019-03-06 16:51:44 +00:00
2019-12-07 21:11:21 +01:00
DB db = DB.getInstance(this);
db.message().liveUnseenNotify().observe(owner, new Observer<List<TupleMessageEx>>() {
2019-03-06 16:51:44 +00:00
@Override
public void onChanged(List<TupleMessageEx> messages) {
2019-07-06 10:16:42 +02:00
if (messages == null)
messages = new ArrayList<>();
int unseen = 0;
for (TupleMessageEx message : messages)
2019-09-27 18:25:55 +02:00
if (!message.ui_seen && !message.ui_ignored && !message.ui_hide)
2019-07-06 10:16:42 +02:00
unseen++;
Log.i("Update tile unseen=" + unseen);
2019-03-06 16:51:44 +00:00
Tile tile = getQsTile();
2020-07-05 21:38:15 +02:00
if (tile != null)
try {
tile.setState(unseen > 0 ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
tile.setIcon(Icon.createWithResource(ServiceTileUnseen.this,
unseen > 0 ? R.drawable.baseline_mail_24 : R.drawable.baseline_mail_outline_24));
tile.setLabel(getResources().getQuantityString(
R.plurals.title_tile_unseen, unseen, unseen));
tile.updateTile();
} catch (Throwable ex) {
Log.w(ex);
// See ServiceTileSynchronize
}
2019-03-06 16:51:44 +00:00
}
});
2018-11-07 08:09:05 +00:00
}
@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() {
2019-03-25 19:20:23 +00:00
owner.destroy();
2018-11-07 08:09:05 +00:00
super.onDestroy();
}
public void onStartListening() {
2018-12-24 12:27:45 +00:00
Log.i("Start tile unseen");
2019-03-06 16:51:44 +00:00
owner.start();
2018-11-07 08:09:05 +00:00
}
public void onStopListening() {
2018-12-24 12:27:45 +00:00
Log.i("Stop tile unseen");
2019-03-06 16:51:44 +00:00
owner.stop();
2018-11-07 08:09:05 +00:00
}
}