Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Fairphone Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.fairphone.updater; |
| 18 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 19 | import java.util.Calendar; |
| 20 | |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 21 | import android.app.AlarmManager; |
| 22 | import android.app.PendingIntent; |
| 23 | import android.content.BroadcastReceiver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Jose Pascoal | 85b803b | 2015-03-19 12:23:11 +0000 | [diff] [blame] | 26 | import android.content.SharedPreferences; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 27 | |
Jose Pascoal | 85b803b | 2015-03-19 12:23:11 +0000 | [diff] [blame] | 28 | import com.fairphone.updater.fragments.MainFragment; |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 29 | import com.fairphone.updater.tools.Utils; |
| 30 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 31 | public class BootBroadcastReceiver extends BroadcastReceiver |
| 32 | { |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 33 | private static final int SERVICE_START_DELAY = 30; |
| 34 | private final static long NOTIFICATION_INTERVAL_MILLIS = 1000 * Utils.SECONDS_IN_MINUTE * Utils.MINUTES_IN_HOUR * 8; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 35 | |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 36 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 37 | @Override |
| 38 | public void onReceive(Context context, Intent intent) |
| 39 | { |
Tiago Costa | 77019fc | 2014-12-03 15:42:08 +0000 | [diff] [blame] | 40 | // |
| 41 | // if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) |
| 42 | // { |
Jose Pascoal | 85b803b | 2015-03-19 12:23:11 +0000 | [diff] [blame] | 43 | SharedPreferences sharedPreferences = context.getSharedPreferences(FairphoneUpdater.FAIRPHONE_UPDATER_PREFERENCES, Context.MODE_PRIVATE); |
| 44 | |
| 45 | SharedPreferences.Editor editor = sharedPreferences.edit(); |
| 46 | editor.putString(FairphoneUpdater.PREFERENCE_CURRENT_UPDATER_STATE, FairphoneUpdater.UpdaterState.NORMAL.name()); |
| 47 | editor.putInt(FairphoneUpdater.PREFERENCE_SELECTED_VERSION_NUMBER, 0); |
| 48 | editor.putString(FairphoneUpdater.PREFERENCE_SELECTED_VERSION_TYPE, ""); |
| 49 | editor.putInt(FairphoneUpdater.PREFERENCE_SELECTED_STORE_NUMBER, -1); |
| 50 | editor.remove(UpdaterService.LAST_CONFIG_DOWNLOAD_IN_MS); |
| 51 | editor.remove(MainFragment.SHARED_PREFERENCES_ENABLE_GAPPS); |
| 52 | editor.commit(); |
| 53 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 54 | AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 55 | Intent i = new Intent(context, UpdaterService.class); |
| 56 | PendingIntent pending = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); |
| 57 | |
Tiago Costa | 77019fc | 2014-12-03 15:42:08 +0000 | [diff] [blame] | 58 | // start service for the first time |
| 59 | context.startService(i); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 60 | Calendar cal = Calendar.getInstance(); |
| 61 | // Start 30 seconds after boot completed |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 62 | cal.add(Calendar.SECOND, SERVICE_START_DELAY); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 63 | |
Tiago Costa | 77019fc | 2014-12-03 15:42:08 +0000 | [diff] [blame] | 64 | // InexactRepeating allows Android to optimize the energy |
| 65 | // consumption |
| 66 | service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), NOTIFICATION_INTERVAL_MILLIS, pending); |
| 67 | // } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 68 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 69 | |
| 70 | } |