Skip to content

Commit

Permalink
fixbug #1
Browse files Browse the repository at this point in the history
  • Loading branch information
frankzhangv5 committed Jun 27, 2019
1 parent 1be0e0f commit 33a8aa6
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 247 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.iq56.poweronofftimer"
minSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
versionCode 4
versionName "1.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":4,"versionName":"1.4","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
13 changes: 6 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iq56.poweronofftimer"
>
<!--android:sharedUserId="android.uid.system"-->
package="com.iq56.poweronofftimer">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:allowBackup="true"
android:directBootAware="true"
Expand All @@ -21,13 +22,11 @@
</intent-filter>
</activity>


<receiver
android:name=".PowerOnOffAlarm"
android:name=".BootReceiver"
android:enabled="true">
<intent-filter android:priority="1000">
<action android:name="com.iq56.poweronofftimer.poweron" />
<action android:name="com.iq56.poweronofftimer.poweroff" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

Expand Down
152 changes: 63 additions & 89 deletions app/src/main/java/com/iq56/poweronofftimer/AlarmService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.iq56.poweronofftimer;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
Expand All @@ -20,6 +17,8 @@ public class AlarmService extends Service {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;

private Api mApi;

@Override
public void onCreate() {
super.onCreate();
Expand All @@ -29,6 +28,8 @@ public void onCreate() {
sharedPreferences = getSharedPreferences(this.getPackageName(), MODE_PRIVATE);
editor = sharedPreferences.edit();

mApi = new Api(this);

Utils.dump(sharedPreferences, "AlarmService onCreate");

setAlarmByService();
Expand Down Expand Up @@ -63,7 +64,7 @@ private void setAlarmByService() {
Utils.dump(sharedPreferences, "beforeSetAlarm");

boolean allowPowerOnOff = sharedPreferences.getBoolean(KEY_ALLOWED_POWER_ONOFF, false);
if(!allowPowerOnOff) {
if (!allowPowerOnOff) {
Log.e(TAG, "not allowed to power on/off");
return;
}
Expand All @@ -75,11 +76,14 @@ private void setAlarmByService() {
setDailyAlarm();

} else if (MODE_WEEKLY.equals(mode)) {

Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
for (int i = 0; i < 7; i++) {
boolean ret = sharedPreferences.getBoolean("repeat_" + WEEKDAYS[i], false);
if (ret) {
setWeeklyAlarm(i + 1);
int index = (i + dayOfWeek - 1) % 7;
boolean checked = sharedPreferences.getBoolean("repeat_" + WEEKDAYS[index], false);
if (checked) {
setWeeklyAlarm(i);
break;
}
}
} else {
Expand All @@ -90,7 +94,14 @@ private void setAlarmByService() {

setDailyAlarm();
}
}

public void clearAllAlarmByService() {
Utils.dump(sharedPreferences, "beforeClearAllAlarm");
mApi.sendClearPowerOnOffBroadcast();
}

private void setDailyAlarm() {
int powerOnHour = sharedPreferences.getInt(KEY_POWER_ON_HOUR, 7);
int powerOnMinute = sharedPreferences.getInt(KEY_POWER_ON_MINUTE, 30);
int powerOffHour = sharedPreferences.getInt(KEY_POWER_OFF_HOUR, 21);
Expand All @@ -100,107 +111,70 @@ private void setAlarmByService() {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);

int[] poweronTime = {year, month , day, powerOnHour, powerOnMinute};
int[] poweroffTime = {year, month , day, powerOffHour, powerOffMinute};
new Api(AlarmService.this).sendSetPowerOnOffBroadcast(poweronTime, poweroffTime);
int[] poweronTime = {year, month, day, powerOnHour, powerOnMinute};
int[] poweroffTime = {year, month, day, powerOffHour, powerOffMinute};

sharedPreferences.edit().putString(KEY_POWERON_TIME, Arrays.toString(poweronTime)).commit();
sharedPreferences.edit().putString(KEY_POWEROFF_TIME, Arrays.toString(poweroffTime)).commit();
}

public void clearAllAlarmByService() {
Utils.dump(sharedPreferences, "beforeClearAllAlarm");
Intent powerOnIntent = new Intent(AlarmService.this, PowerOnOffAlarm.class);
powerOnIntent.setAction(ACTION_POWER_ON);
PendingIntent powerOnSender = PendingIntent.getBroadcast(AlarmService.this,
REQUEST_CODE_POWER_ON_OFF, powerOnIntent, PendingIntent.FLAG_NO_CREATE);
if (powerOnHour < hour || (powerOnHour == hour && powerOnMinute < minute)) {
poweronTime[2] += 1;
}

// Schedule the alarm!
AlarmManager am = (AlarmManager) AlarmService.this
.getSystemService(Context.ALARM_SERVICE);
am.cancel(powerOnSender);
if (powerOffHour < hour || (powerOffHour == hour && powerOffMinute < minute)) {
poweroffTime[2] += 1;
}

Intent powerOffIntent = new Intent(AlarmService.this, PowerOnOffAlarm.class);
powerOffIntent.setAction(ACTION_POWER_OFF);
PendingIntent powerOffSender = PendingIntent.getBroadcast(AlarmService.this,
REQUEST_CODE_POWER_ON_OFF, powerOffIntent, PendingIntent.FLAG_NO_CREATE);
mApi.sendClearPowerOnOffBroadcast();

am.cancel(powerOffSender);
sharedPreferences.edit().putString(KEY_POWERON_TIME, Arrays.toString(poweronTime)).commit();
sharedPreferences.edit().putString(KEY_POWEROFF_TIME, Arrays.toString(poweroffTime)).commit();

new Api(AlarmService.this).sendClearPowerOnOffBroadcast();
mApi.sendSetPowerOnOffBroadcast(poweronTime, poweroffTime);
}

private void setDailyAlarm() {
private void setWeeklyAlarm(int deltaDays) {
int powerOnHour = sharedPreferences.getInt(KEY_POWER_ON_HOUR, 7);
int powerOnMinute = sharedPreferences.getInt(KEY_POWER_ON_MINUTE, 30);
int powerOffHour = sharedPreferences.getInt(KEY_POWER_OFF_HOUR, 21);
int powerOffMinute = sharedPreferences.getInt(KEY_POWER_OFF_MINUTE, 30);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, powerOnHour);
calendar.set(Calendar.MINUTE, powerOnMinute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

Intent powerOnIntent = new Intent(AlarmService.this, PowerOnOffAlarm.class);
powerOnIntent.setAction(ACTION_POWER_ON);
PendingIntent powerOnSender = PendingIntent.getBroadcast(AlarmService.this,
REQUEST_CODE_POWER_ON_OFF, powerOnIntent, PendingIntent.FLAG_CANCEL_CURRENT);

// Schedule the alarm!
AlarmManager am = (AlarmManager) AlarmService.this
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
ONE_DAY_INTERVAL, powerOnSender);

Intent powerOffIntent = new Intent(AlarmService.this, PowerOnOffAlarm.class);
powerOffIntent.setAction(ACTION_POWER_OFF);
PendingIntent powerOffSender = PendingIntent.getBroadcast(AlarmService.this,
REQUEST_CODE_POWER_ON_OFF, powerOffIntent, PendingIntent.FLAG_CANCEL_CURRENT);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH) + deltaDays;
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);

calendar.set(Calendar.HOUR_OF_DAY, powerOffHour);
calendar.set(Calendar.MINUTE, powerOffMinute);
int[] poweronTime = {year, month, day, powerOnHour, powerOnMinute};
int[] poweroffTime = {year, month, day, powerOffHour, powerOffMinute};

am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
ONE_DAY_INTERVAL, powerOffSender);
if (deltaDays == 0) {
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int delta = 0;
for (int i = 0; i < 7; i++) {
int index = (i + dayOfWeek - 1) % 7;
boolean checked = sharedPreferences.getBoolean("repeat_" + WEEKDAYS[index], false);
if (checked && i > 0) {
delta = i;
break;
}
}
if (powerOnHour < hour || (powerOnHour == hour && powerOnMinute < minute)) {
poweronTime[2] += delta;
}

if (powerOffHour < hour || (powerOffHour == hour && powerOffMinute < minute)) {
poweroffTime[2] += delta;
}
}

}
mApi.sendClearPowerOnOffBroadcast();

private void setWeeklyAlarm(int dayOfWeek) {
int powerOnHour = sharedPreferences.getInt(KEY_POWER_ON_HOUR, 7);
int powerOnMinute = sharedPreferences.getInt(KEY_POWER_ON_MINUTE, 30);
int powerOffHour = sharedPreferences.getInt(KEY_POWER_OFF_HOUR, 21);
int powerOffMinute = sharedPreferences.getInt(KEY_POWER_OFF_MINUTE, 30);
sharedPreferences.edit().putString(KEY_POWERON_TIME, Arrays.toString(poweronTime)).commit();
sharedPreferences.edit().putString(KEY_POWEROFF_TIME, Arrays.toString(poweroffTime)).commit();

final Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
calendar.set(Calendar.HOUR_OF_DAY, powerOnHour);
calendar.set(Calendar.MINUTE, powerOnMinute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);

Intent powerOnIntent = new Intent(AlarmService.this, PowerOnOffAlarm.class);
powerOnIntent.setAction(ACTION_POWER_ON);
PendingIntent powerOnSender = PendingIntent.getBroadcast(AlarmService.this,
REQUEST_CODE_POWER_ON_OFF, powerOnIntent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager am = (AlarmManager) AlarmService.this
.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
ONE_WEEK_INTERVAL, powerOnSender);


calendar.set(Calendar.HOUR_OF_DAY, powerOffHour);
calendar.set(Calendar.MINUTE, powerOffMinute);

Intent powerOffIntent = new Intent(AlarmService.this, PowerOnOffAlarm.class);
powerOffIntent.setAction(ACTION_POWER_OFF);
PendingIntent powerOffSender = PendingIntent.getBroadcast(AlarmService.this,
REQUEST_CODE_POWER_ON_OFF, powerOffIntent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
ONE_WEEK_INTERVAL, powerOffSender);
mApi.sendSetPowerOnOffBroadcast(poweronTime, poweroffTime);
}

@Override
Expand Down
23 changes: 0 additions & 23 deletions app/src/main/java/com/iq56/poweronofftimer/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.util.Arrays;


import static com.iq56.poweronofftimer.Utils.KEY_POWEROFF_TIME;
import static com.iq56.poweronofftimer.Utils.KEY_POWERON_TIME;
import static com.iq56.poweronofftimer.Utils.TAG;
Expand All @@ -29,17 +28,6 @@ public Api(Context context) {
mContext = context;
}

/**
* @param datetime [year,month,day, hour,minute]
*/
public void sendSetPowerOnBroadcast(int[] datetime) {
Log.i(TAG, "sendSetPowerOnBroadcast : " + Arrays.toString(datetime));
Intent intent = new Intent(ACTION_SET_POWERONOFF);
intent.putExtra(EXTRA_TIME_ON, datetime);
intent.putExtra(EXTRA_ENABLE, true);
mContext.sendBroadcast(intent);
}

/**
* @param poweronTime [year,month,day, hour,minute]
*/
Expand All @@ -52,17 +40,6 @@ public void sendSetPowerOnOffBroadcast(int[] poweronTime, int[] poweroffTime) {
mContext.sendBroadcast(intent);
}

/**
* @param datetime [year,month,day, hour,minute]
*/
public void sendSetPowerOffBroadcast(int[] datetime) {
Log.i(TAG, "sendSetPowerOffBroadcast : " + Arrays.toString(datetime));
Intent intent = new Intent(ACTION_SET_POWERONOFF);
intent.putExtra(EXTRA_TIME_OFF, datetime);
intent.putExtra(EXTRA_ENABLE, true);
mContext.sendBroadcast(intent);
}

public void sendClearPowerOnOffBroadcast() {
SharedPreferences sharedPreferences = mContext.getSharedPreferences(mContext.getPackageName(), Context.MODE_PRIVATE);

Expand Down
25 changes: 25 additions & 0 deletions app/src/main/java/com/iq56/poweronofftimer/BootReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.iq56.poweronofftimer;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import static com.iq56.poweronofftimer.Utils.TAG;

public class BootReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

String action = intent.getAction();
Log.i(TAG, "onReceive: " + intent);

if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
Log.i(TAG, "starting AlarmService ...");
context.startService(new Intent(context, AlarmService.class));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
}


@Override
public void onClick(View v) {
switch (v.getId()) {
Expand Down
Loading

0 comments on commit 33a8aa6

Please sign in to comment.