This repository was archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathMainActivity.java
More file actions
562 lines (494 loc) · 21.3 KB
/
Copy pathMainActivity.java
File metadata and controls
562 lines (494 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package info.papdt.blackblub.ui;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.*;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.github.zagum.expandicon.ExpandIconView;
import info.papdt.blackblub.Constants;
import info.papdt.blackblub.IMaskServiceInterface;
import info.papdt.blackblub.R;
import info.papdt.blackblub.receiver.ActionReceiver;
import info.papdt.blackblub.service.MaskService;
import info.papdt.blackblub.ui.adapter.ModeListAdapter;
import info.papdt.blackblub.ui.dialog.SchedulerDialog;
import info.papdt.blackblub.util.AlarmUtil;
import info.papdt.blackblub.util.Settings;
import info.papdt.blackblub.util.Utility;
import moe.feng.alipay.zerosdk.AlipayZeroSdk;
import java.util.List;
public class MainActivity extends Activity {
// Views & States
private ImageButton mToggle;
private SeekBar mSeekBar;
private ExpandIconView mExpandIcon;
private View mDivider, mMiniSchedulerBar;
private TextView mMiniSchedulerStatus, mButtonTip;
private View mSchedulerRow;
private TextView mSchedulerStatus;
private ImageView mSchedulerIcon;
private View mAdvancedModeRow;
private TextView mAdvancedModeText;
private View mYellowFilterRow;
private SeekBar mYellowFilterSeekBar;
private View mMoreSettingsRow;
private AlertDialog mFirstRunDialog;
private boolean isUsingDarkTheme = false;
private static boolean isExpand = false, hasDismissFirstRunDialog = false;
// Service states
private boolean isRunning = false;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IMaskServiceInterface msi = IMaskServiceInterface.Stub.asInterface(service);
try {
setToggleIconState(isRunning = msi.isShowing());
Utility.createStatusBarTiles(MainActivity.this, isRunning);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {}
};
// Settings
private Settings mSettings;
// Local broadcast receivers
private MessageReceiver mReceiver;
// Constants
private static final int REQUEST_CODE_OVERLAY_PERMISSION = 1001;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
mSettings = Settings.getInstance(this);
if (!mSettings.shouldShowTask()) {
final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am != null) {
List<ActivityManager.AppTask> tasks = am.getAppTasks();
if (tasks != null && tasks.size() > 0) {
for (ActivityManager.AppTask task : tasks) {
task.setExcludeFromRecents(true);
}
}
}
}
// Apply theme and transparent system ui
Utility.applyTransparentSystemUI(this);
if (mSettings.isDarkTheme()) {
setTheme(R.style.AppTheme_Dark);
isUsingDarkTheme = true;
} else {
setTheme(R.style.AppTheme_Light);
isUsingDarkTheme = false;
}
// Apply Noto Sans CJK Full font from FontProvider API
Utility.applyNotoSansCJK(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up rootView & aboutButton's click event
findViewById(R.id.root_layout).setOnClickListener(v -> {
// When rootView is clicked, exit main activity.
finish();
});
// Set up cardView's top padding and system ui visibility
LinearLayout cardView = findViewById(R.id.card_view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !mSettings.isDarkTheme()) {
// PS: When Blackbulb runs on pre-23 API, it will be hard to see status bar icons
// because of light background. I don't want to fix it. User experience requires
// users to keep system version not out-of-date.
cardView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
cardView.setPadding(0, Utility.getStatusBarHeight(this), 0, 0);
// Set up toggle
mToggle = findViewById(R.id.toggle);
mToggle.setOnClickListener(v -> {
if (!isRunning) {
if (!Utility.canDrawOverlays(this)) {
Utility.requestOverlayPermission(
this, REQUEST_CODE_OVERLAY_PERMISSION);
return;
}
startMaskService();
} else {
stopMaskService();
}
});
// Set up seekBar
mSeekBar = findViewById(R.id.seek_bar);
setSeekBarProgress(mSettings.getBrightness(60) - 20);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int currentProgress = -1;
@Override public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
currentProgress = progress + 20;
if (isRunning) {
// Only send broadcast when running
Intent intent = new Intent(MainActivity.this, MaskService.class);
intent.putExtra(Constants.Extra.ACTION, Constants.Action.UPDATE);
intent.putExtra(Constants.Extra.BRIGHTNESS, currentProgress);
startService(intent);
} else if (mSettings.isAutoEnableWhenBrightnessChanged()) {
if (!Utility.canDrawOverlays(MainActivity.this)) {
return;
}
startMaskService();
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (currentProgress != -1) {
mSettings.setBrightness(currentProgress);
}
}
});
// Set up expandIcon
mExpandIcon = findViewById(R.id.expand_icon);
mExpandIcon.setOnClickListener(v -> {
// Change the states of expandable views
isExpand = !isExpand;
updateExpandViews();
});
mMiniSchedulerBar = findViewById(R.id.mini_scheduler_info);
mMiniSchedulerStatus = findViewById(R.id.mini_scheduler_status_text);
mDivider = findViewById(R.id.divider_line);
// Show/Hide button tip
mButtonTip = findViewById(R.id.button_tips);
if (!mSettings.needButtonTip()) mButtonTip.setVisibility(View.GONE);
// Init rows (Better not change initialization orders)
initSchedulerRow();
initYellowFilterRow();
initAdvancedModeRow();
initMoreSettingsRow();
updateExpandViews();
}
private void updateExpandViews() {
mExpandIcon.setState(isExpand ? ExpandIconView.LESS : ExpandIconView.MORE, true);
int visibility = isExpand ? View.VISIBLE : View.GONE;
mMiniSchedulerBar.setVisibility(
!isExpand && mSettings.isAutoMode() ? View.VISIBLE : View.GONE);
mDivider.setVisibility(visibility);
mSchedulerRow.setVisibility(visibility);
mYellowFilterRow.setVisibility(visibility);
mAdvancedModeRow.setVisibility(visibility);
mMoreSettingsRow.setVisibility(visibility);
}
private void initSchedulerRow() {
mSchedulerRow = findViewById(R.id.scheduler_row);
mSchedulerIcon = findViewById(R.id.scheduler_icon);
mSchedulerStatus = findViewById(R.id.tv_scheduler_status);
mSchedulerRow.setOnClickListener(v -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PowerManager pm = getSystemService(PowerManager.class);
if (pm != null && !pm.isIgnoringBatteryOptimizations(getPackageName())) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.dialog_ignore_battery_opt_title)
.setMessage(R.string.dialog_ignore_battery_opt_msg)
.setPositiveButton(android.R.string.ok, (d, i) -> showSchedulerDialog())
.setNeutralButton(R.string.dialog_button_go_to_set,
(d, i) -> Utility.requestBatteryOptimization(this))
.show();
return;
}
}
showSchedulerDialog();
});
updateSchedulerRow();
}
private void initYellowFilterRow() {
mYellowFilterRow = findViewById(R.id.yellow_filter_row);
mYellowFilterSeekBar = findViewById(R.id.yellow_filter_seek_bar);
mYellowFilterSeekBar.setProgress(mSettings.getYellowFilterAlpha());
mYellowFilterSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
private int currentProgress = -1;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (!fromUser) return;
currentProgress = progress;
if (isRunning) {
// Only send broadcast when running
Intent intent = new Intent(MainActivity.this, MaskService.class);
intent.putExtra(Constants.Extra.ACTION, Constants.Action.UPDATE);
intent.putExtra(Constants.Extra.YELLOW_FILTER_ALPHA, currentProgress);
startService(intent);
}
}
@Override public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (currentProgress != -1) {
mSettings.setYellowFilterAlpha(currentProgress);
}
}
});
}
private void updateSchedulerRow() {
mSchedulerIcon.setImageResource(mSettings.isAutoMode() ?
R.drawable.ic_alarm_black_24dp : R.drawable.ic_alarm_off_black_24dp);
if (mSettings.isAutoMode()) {
String text;
if (isRunning && AlarmUtil.isInSleepTime(this)) {
text = getString(R.string.scheduler_status_on_show_disable_time,
mSettings.getSunriseTimeText());
} else {
text = getString(R.string.scheduler_status_on_show_enable_time,
mSettings.getSunsetTimeText());
}
mSchedulerStatus.setText(text);
mMiniSchedulerStatus.setText(text);
} else {
mSchedulerStatus.setText(R.string.scheduler_status_off);
}
}
private void initAdvancedModeRow() {
mAdvancedModeRow = findViewById(R.id.advanced_mode_row);
mAdvancedModeText = findViewById(R.id.tv_mode_status);
mAdvancedModeRow.setOnClickListener(v -> showAdvancedModeDialog());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Show explanation
mAdvancedModeRow.setOnClickListener(o ->
new AlertDialog.Builder(this)
.setTitle(R.string.mode_android_oreo_explanation_dialog_title)
.setMessage(R.string.mode_android_oreo_explanation_dialog_message)
.setNeutralButton(R.string.mode_android_oreo_explanation_read_more,
(d, w) -> startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(
getString(
R.string.mode_android_oreo_explanation_url)
)))
)
.setPositiveButton(android.R.string.ok, (d, w) -> {})
.show()
);
}
updateAdvancedModeRow();
}
private void updateAdvancedModeRow() {
// Color filter is not supported in overlay all mode.
mYellowFilterSeekBar.setEnabled(
mSettings.getAdvancedMode() != Constants.AdvancedMode.OVERLAY_ALL);
mYellowFilterSeekBar.setProgress(
mSettings.getAdvancedMode() != Constants.AdvancedMode.OVERLAY_ALL ?
mSettings.getYellowFilterAlpha() : 0);
int textResId;
switch (mSettings.getAdvancedMode()) {
case Constants.AdvancedMode.NONE:
textResId = R.string.mode_text_normal;
break;
case Constants.AdvancedMode.NO_PERMISSION:
textResId = R.string.mode_text_no_permission;
break;
case Constants.AdvancedMode.OVERLAY_ALL:
textResId = R.string.mode_text_overlay_all;
break;
default:
throw new IllegalStateException("Unsupported advanced mode.");
}
mAdvancedModeText.setText(textResId);
}
private void initMoreSettingsRow() {
mMoreSettingsRow = findViewById(R.id.more_settings_row);
final View button = findViewById(R.id.btn_more_settings);
button.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, MoreSettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
});
mMoreSettingsRow.setOnClickListener(v -> button.performClick());
}
private void showSchedulerDialog() {
new SchedulerDialog(this, dialogInterface -> updateSchedulerRow()).show();
}
private void showAdvancedModeDialog() {
int current = mSettings.getAdvancedMode();
new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.dialog_choose_mode)
.setSingleChoiceItems(new ModeListAdapter(current), current, (dialog, which) -> {
AlertDialog modeDialog = (AlertDialog) dialog;
ModeListAdapter adapter =
(ModeListAdapter) modeDialog.getListView().getAdapter();
// Set mode value
mSettings.setAdvancedMode(adapter.getItem(which).getModeId());
updateAdvancedModeRow();
// Restart service
mToggle.performClick();
mToggle.postDelayed(() -> mToggle.performClick(), 800);
dialog.dismiss();
})
.setNegativeButton(android.R.string.cancel, null)
.show();
}
@Override
protected void onResume() {
super.onResume();
if (mSettings.isDarkTheme() != isUsingDarkTheme) {
isUsingDarkTheme = mSettings.isDarkTheme();
new Handler(Looper.getMainLooper()).postDelayed(this::recreate, 200);
return;
}
if (mReceiver == null) {
mReceiver = new MessageReceiver();
}
registerReceiver(mReceiver, new IntentFilter(Constants.ACTION_TOGGLE));
// Request current state
bindService(new Intent(this, MaskService.class),
mServiceConnection, MaskService.BIND_AUTO_CREATE);
}
@Override
protected void onPause() {
super.onPause();
if (mReceiver != null) {
try {
unregisterReceiver(mReceiver);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
unbindService(mServiceConnection);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_OVERLAY_PERMISSION) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Utility.canDrawOverlays(this)) {
startMaskService();
}
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (mSettings.shouldHandleVolumeKey()) {
// Support control brightness by volume buttons
int action = event.getAction();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
setSeekBarProgress(mSeekBar.getProgress() - 5);
}
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
setSeekBarProgress(mSeekBar.getProgress() + 5);
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
private void setSeekBarProgress(int progress) {
progress = Math.max(0, Math.min(80, progress));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mSeekBar.setProgress(progress, true);
} else {
mSeekBar.setProgress(progress);
}
}
private void setToggleIconState(boolean isRunning) {
if (mToggle != null && !isFinishing()) {
updateSchedulerRow();
mToggle.setImageResource(isRunning ?
R.drawable.ic_brightness_2_black_24dp : R.drawable.ic_brightness_7_black_24dp);
}
}
private void startMaskService() {
if (mSettings.needButtonTip()) {
mSettings.setNeedButtonTip(false);
mButtonTip.setVisibility(View.GONE);
}
// Send start action
ActionReceiver.sendActionStart(this);
setToggleIconState(isRunning = true);
// For safe
if (mSettings.isFirstRun()) {
if (mFirstRunDialog != null && mFirstRunDialog.isShowing()) {
return;
}
hasDismissFirstRunDialog = false;
mFirstRunDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.dialog_first_run_title)
.setMessage(R.string.dialog_first_run_message)
.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
hasDismissFirstRunDialog = true;
mSettings.setFirstRun(false);
})
.setOnDismissListener(dialogInterface -> {
if (hasDismissFirstRunDialog) return;
hasDismissFirstRunDialog = true;
if (mSettings.isFirstRun()) {
Intent intent1 =
new Intent(MainActivity.this, MaskService.class);
intent1.putExtra(Constants.Extra.ACTION, Constants.Action.STOP);
stopService(intent1);
setToggleIconState(isRunning = false);
}
})
.show();
new Handler().postDelayed(() -> {
if (mFirstRunDialog.isShowing() && !hasDismissFirstRunDialog) {
mFirstRunDialog.dismiss();
}
}, 5000);
}
}
private void stopMaskService() {
Intent intent = new Intent(MainActivity.this, MaskService.class);
intent.putExtra(Constants.Extra.ACTION, Constants.Action.STOP);
startService(intent);
setToggleIconState(isRunning = false);
}
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (mToggle == null) return;
int eventId = intent.getIntExtra(Constants.Extra.EVENT_ID, -1);
switch (eventId) {
case Constants.Event.CANNOT_START:
// Receive a error from MaskService
isRunning = false;
setToggleIconState(false);
if (!isFinishing()) {
Toast.makeText(
context.getApplicationContext(),
R.string.mask_fail_to_start,
Toast.LENGTH_LONG
).show();
}
break;
case Constants.Event.DESTROY_SERVICE:
// MaskService is destroying
if (isRunning) {
setToggleIconState(false);
isRunning = false;
}
break;
case Constants.Event.CHECK:
// Receive check event and update toggle icon state
isRunning = intent.getBooleanExtra(Constants.Extra.IS_SHOWING, false);
Log.i(TAG, "Checked " + isRunning);
setToggleIconState(isRunning);
break;
}
}
}
}