Skip to content

Commit b2a834e

Browse files
committed
refactor tile service
1 parent 8df12ee commit b2a834e

File tree

2 files changed

+120
-119
lines changed

2 files changed

+120
-119
lines changed

app/src/main/java/io/github/ratul/topactivity/service/QuickSettingsTileService.java

Lines changed: 0 additions & 119 deletions
This file was deleted.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (C) 2022 Ratul Hasan
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
package io.github.ratul.topactivity.services;
18+
19+
import static android.app.PendingIntent.FLAG_IMMUTABLE;
20+
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
21+
22+
import android.annotation.SuppressLint;
23+
import android.app.PendingIntent;
24+
import android.content.BroadcastReceiver;
25+
import android.content.ComponentName;
26+
import android.content.Context;
27+
import android.content.Intent;
28+
import android.content.IntentFilter;
29+
import android.os.Build;
30+
import android.service.quicksettings.Tile;
31+
import android.service.quicksettings.TileService;
32+
33+
import androidx.core.content.ContextCompat;
34+
35+
import io.github.ratul.topactivity.receivers.NotificationReceiver;
36+
import io.github.ratul.topactivity.ui.MainActivity;
37+
import io.github.ratul.topactivity.utils.DatabaseUtil;
38+
import io.github.ratul.topactivity.utils.WindowUtil;
39+
40+
/**
41+
* Created by Wen on 5/3/16.
42+
* Refactored by Ratul on 04/05/2022.
43+
*/
44+
public class QuickSettingsTileService extends TileService {
45+
private static final String ACTION_UPDATE_TITLE = "io.github.ratul.topactivity.ACTION_UPDATE_TILE";
46+
private UpdateTileReceiver mReceiver;
47+
48+
public static void updateTile(Context context) {
49+
TileService.requestListeningState(context.getApplicationContext(),
50+
new ComponentName(context, QuickSettingsTileService.class));
51+
context.sendBroadcast(new Intent(QuickSettingsTileService.ACTION_UPDATE_TITLE));
52+
}
53+
54+
public void updateTile() {
55+
getQsTile().setState(DatabaseUtil.isShowingWindow() ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
56+
getQsTile().updateTile();
57+
}
58+
59+
@Override
60+
public void onCreate() {
61+
super.onCreate();
62+
mReceiver = new UpdateTileReceiver();
63+
}
64+
65+
@Override
66+
public void onTileAdded() {
67+
sendBroadcast(new Intent(MainActivity.ACTION_STATE_CHANGED));
68+
}
69+
70+
@Override
71+
public void onTileRemoved() {
72+
super.onTileRemoved();
73+
sendBroadcast(new Intent(MainActivity.ACTION_STATE_CHANGED));
74+
}
75+
76+
@Override
77+
public void onStartListening() {
78+
ContextCompat.registerReceiver(getApplicationContext(), mReceiver,
79+
new IntentFilter(ACTION_UPDATE_TITLE), ContextCompat.RECEIVER_EXPORTED);
80+
updateTile();
81+
super.onStartListening();
82+
}
83+
84+
@Override
85+
public void onStopListening() {
86+
getApplicationContext().unregisterReceiver(mReceiver);
87+
super.onStopListening();
88+
}
89+
90+
@SuppressLint("StartActivityAndCollapseDeprecated")
91+
@Override
92+
public void onClick() {
93+
if (DatabaseUtil.isShowingWindow() && WindowUtil.isViewVisible()) {
94+
DatabaseUtil.setShowingWindow(false);
95+
NotificationReceiver.cancelNotification();
96+
WindowUtil.dismiss(this);
97+
sendBroadcast(new Intent(MainActivity.ACTION_STATE_CHANGED));
98+
updateTile();
99+
return;
100+
}
101+
102+
Intent intent = new Intent(this, MainActivity.class)
103+
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
104+
.putExtra(MainActivity.EXTRA_FROM_QS_TILE, true);
105+
106+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
107+
startActivityAndCollapse(PendingIntent.getActivity(this,
108+
7456435, intent, FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE));
109+
} else {
110+
startActivityAndCollapse(intent);
111+
}
112+
}
113+
114+
class UpdateTileReceiver extends BroadcastReceiver {
115+
@Override
116+
public void onReceive(Context context, Intent intent) {
117+
updateTile();
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)