blob: c777652927412c68fa5808510b98869fcdbddfcb [file] [log] [blame]
Kim Hansen086964d2013-12-10 11:33:28 +00001/*
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
17package com.fairphone.updater;
18
Jose Pascoal810950b2014-10-09 17:16:08 +010019import java.util.Calendar;
20
Kim Hansen086964d2013-12-10 11:33:28 +000021import android.app.AlarmManager;
22import android.app.PendingIntent;
23import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
Jose Pascoal85b803b2015-03-19 12:23:11 +000026import android.content.SharedPreferences;
Kim Hansen086964d2013-12-10 11:33:28 +000027
Jose Pascoal85b803b2015-03-19 12:23:11 +000028import com.fairphone.updater.fragments.MainFragment;
Jose Pascoal46fdb062015-02-05 18:59:32 +000029import com.fairphone.updater.tools.Utils;
30
Jose Pascoal810950b2014-10-09 17:16:08 +010031public class BootBroadcastReceiver extends BroadcastReceiver
32{
Jose Pascoal46fdb062015-02-05 18:59:32 +000033 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 Hansen086964d2013-12-10 11:33:28 +000035
Kim Hansen086964d2013-12-10 11:33:28 +000036
Jose Pascoal810950b2014-10-09 17:16:08 +010037 @Override
38 public void onReceive(Context context, Intent intent)
39 {
Tiago Costa77019fc2014-12-03 15:42:08 +000040 //
41 // if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()))
42 // {
Jose Pascoal85b803b2015-03-19 12:23:11 +000043 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 Pascoal810950b2014-10-09 17:16:08 +010054 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 Costa77019fc2014-12-03 15:42:08 +000058 // start service for the first time
59 context.startService(i);
Jose Pascoal810950b2014-10-09 17:16:08 +010060 Calendar cal = Calendar.getInstance();
61 // Start 30 seconds after boot completed
Jose Pascoal46fdb062015-02-05 18:59:32 +000062 cal.add(Calendar.SECOND, SERVICE_START_DELAY);
Jose Pascoal810950b2014-10-09 17:16:08 +010063
Tiago Costa77019fc2014-12-03 15:42:08 +000064 // InexactRepeating allows Android to optimize the energy
65 // consumption
66 service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), NOTIFICATION_INTERVAL_MILLIS, pending);
67 // }
Jose Pascoal810950b2014-10-09 17:16:08 +010068 }
Kim Hansen086964d2013-12-10 11:33:28 +000069
70}