Skip to content
This repository was archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
add pause/resume function
Browse files Browse the repository at this point in the history
1.add pause/resume function to foreground notification
2.add "swipe to close" function to paused notification
3.find bug and add todo comment "todo: fix bug that launcherActivity
will start twice"
4. need somebody to update value/strings  (fr,tr,sp)
  • Loading branch information
ArchieMeng committed Dec 26, 2016
1 parent 39bccce commit ec2ab99
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ public void onReceive(Context context, Intent intent) {
intent1.putExtra(C.EXTRA_BRIGHTNESS, Settings.getInstance(context).getInt(Settings.KEY_BRIGHTNESS, brightness));
context.startService(intent1);
break;
//Todo fix bug: receiver never receive ACTION_PAUSE which is the value of "turn off" action in notification
case C.ACTION_PAUSE:
Intent intent2 = new Intent(context, MaskService.class);
intent2.putExtra(C.EXTRA_ACTION, C.ACTION_PAUSE);
intent2.putExtra(C.EXTRA_BRIGHTNESS, Settings.getInstance(context).getInt(Settings.KEY_BRIGHTNESS, brightness));
context.stopService(intent2);
context.startService(intent2);
break;
case C.ACTION_STOP:
Expand Down
28 changes: 17 additions & 11 deletions app/src/main/java/info/papdt/blackblub/services/MaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private void createMaskView() {
}

private void createNotification() {
Log.i(TAG, "Create running notification");
Intent openIntent = new Intent(this, LaunchActivity.class);
Intent pauseIntent = new Intent();
pauseIntent.setAction(TileReceiver.ACTION_UPDATE_STATUS);
Expand All @@ -152,17 +153,17 @@ private void createNotification() {
Notification.Action pauseAction = new Notification.Action(
R.drawable.ic_wb_incandescent_black_24dp,
getString(R.string.notification_action_turn_off),
PendingIntent.getBroadcast(getApplicationContext(), 0, pauseIntent, PendingIntent.FLAG_CANCEL_CURRENT)
PendingIntent.getBroadcast(getBaseContext(), 0, pauseIntent, Intent.FILL_IN_DATA)
);

mNoti = new Notification.Builder(getApplicationContext())
.setContentTitle(getString(R.string.notification_running_title))
.setContentText(getString(R.string.notification_running_msg))
.setSmallIcon(R.drawable.ic_brightness_2_white_36dp)
.addAction(pauseAction)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, openIntent, PendingIntent.FLAG_CANCEL_CURRENT))
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(false)
.setOngoing(false)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setShowWhen(false)
.build();
Expand All @@ -171,27 +172,31 @@ private void createNotification() {

// implement pause notification
private void createPauseNotification(){
Log.i(TAG, "Create pause notification");
Log.i(TAG, "Create paused notification");
Intent openIntent = new Intent(this, LaunchActivity.class);
Intent resumeIntent = new Intent();
resumeIntent.setAction(TileReceiver.ACTION_UPDATE_STATUS);
resumeIntent.putExtra(C.EXTRA_ACTION, C.ACTION_START);
resumeIntent.putExtra(C.EXTRA_BRIGHTNESS, brightness);

Intent closeIntent = new Intent(this, MaskService.class);
closeIntent.putExtra(C.EXTRA_ACTION, C.ACTION_STOP);

Notification.Action resumeAction = new Notification.Action(R.drawable.ic_wb_incandescent_black_24dp,
getString(R.string.notification_action_turn_on),
PendingIntent.getBroadcast(getApplicationContext(), 0, resumeIntent, PendingIntent.FLAG_CANCEL_CURRENT));
PendingIntent.getBroadcast(getBaseContext(), 0, resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT));

mNoti = new Notification.Builder(getApplicationContext())
.setContentTitle(getString(R.string.notification_paused_title))
.setContentText(getString(R.string.notification_paused_msg))
.setSmallIcon(R.drawable.ic_brightness_2_white_36dp)
.addAction(resumeAction)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, openIntent, PendingIntent.FLAG_CANCEL_CURRENT))
.setAutoCancel(false)
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(true)
.setOngoing(false)
.setOnlyAlertOnce(true)
.setShowWhen(false)
.setDeleteIntent(PendingIntent.getService(getBaseContext(), 0, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.build();
}

Expand All @@ -202,7 +207,7 @@ private void showNotification() {
mNotificationManager.notify(NOTIFICATION_NO, mNoti);
}

private void showPausedNotification() {
private void showPausedNotification(){
if (mNoti == null) {
createPauseNotification();
}
Expand Down Expand Up @@ -234,24 +239,25 @@ public int onStartCommand(Intent intent, int flags, int arg) {
}
isShowing = true;
mSettings.putBoolean(Settings.KEY_ALIVE, true);
cancelNotification();
showNotification();
createNotification();
startForeground(NOTIFICATION_NO, mNoti);
try {
Utility.createStatusBarTiles(this, true);
mWindowManager.updateViewLayout(mLayout, mLayoutParams);
mLayout.animate()
.alpha(targetAlpha)
.setDuration(ANIMATE_DURATION_MILES)
.start();
Utility.createStatusBarTiles(this, true);
} catch (Exception e) {
// do nothing....
}
Log.i(TAG, "Set alpha:" + String.valueOf(100 - intent.getIntExtra(C.EXTRA_BRIGHTNESS, 0)));
break;
case C.ACTION_PAUSE:
Log.i(TAG, "Pause Mask");
stopForeground(true);
cancelNotification();
createPauseNotification();
showPausedNotification();
isShowing = false;
mLayout.setAlpha(0f);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<string name="notification_running_title">Night Screen está activo</string>
<string name="notification_running_msg">Toca aquí para abrir los ajustes</string>
<string name="notification_action_turn_off">Desactivar</string>
<string name="notification_action_turn_on">Encender</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<string name="notification_running_title">Night Screen est en exécution</string>
<string name="notification_running_msg">Appuyez pour ouvrir les paramètres</string>
<string name="notification_action_turn_off">Désactiver</string>
<string name="notification_action_turn_on">Allumer</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-tr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<string name="notification_running_title">Night Screen çalışıyor.</string>
<string name="notification_running_msg">Buraya tıklayarak Ayarları açabilirsiniz.</string>
<string name="notification_action_turn_off">Kapat</string>
<string name="notification_action_turn_on">açmak</string>

</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<string name="notification_running_msg">点击通知打开设置</string>

<string name="notification_paused_title">夜间屏幕已暂停</string>
<string name="notification_paused_msg">点击通知打开设置</string>
<string name="notification_paused_msg">滑动关闭夜间屏幕 或 点击通知打开设置</string>

<string name="notification_action_turn_off">开灯</string>
<string name="notification_action_turn_on">关灯</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

<string name="notification_running_title">夜間螢幕正運作</string>
<string name="notification_running_msg">點擊通知打開設定</string>


<string name="notification_action_turn_off">開燈</string>
<string name="notification_paused_title">夜間屏幕已暫停</string>
<string name="notification_paused_msg">滑動關閉夜間屏幕 或 點擊通知打開設置</string>
<string name="notification_action_turn_on">關燈</string>

</resources>
8 changes: 6 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<resources>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation" >

<string name="app_name">Night Screen</string>

Expand All @@ -23,8 +25,10 @@

<string name="notification_running_title">Night Screen is working</string>
<string name="notification_running_msg">Click here to open settings</string>

<string name="notification_paused_title">Night Screen is paused</string>
<string name="notification_paused_msg">Click here to open settings</string>
<string name="notification_paused_msg">Click here to open settings or Swipe to stop Night Screen</string>

<string name="notification_action_turn_off">Turn off</string>
<string name="notification_action_turn_on">Turn on</string>

Expand Down
15 changes: 0 additions & 15 deletions app/src/test/java/info/papdt/blackblub/ExampleUnitTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.android.tools.build:gradle:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Oct 21 11:34:03 PDT 2015
#Mon Dec 26 18:03:15 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

0 comments on commit ec2ab99

Please sign in to comment.