blob: 49cba1fa445180052f32adf2d917be0237de192f [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 */
Jose Pascoalac6a8462014-09-10 20:12:08 +010016package com.fairphone.updater.gappsinstaller;
Kim Hansen086964d2013-12-10 11:33:28 +000017
Kim Hansen086964d2013-12-10 11:33:28 +000018import android.appwidget.AppWidgetManager;
Kim Hansen086964d2013-12-10 11:33:28 +000019import android.content.ComponentName;
20import android.content.Context;
Kim Hansen086964d2013-12-10 11:33:28 +000021import android.content.SharedPreferences;
Kim Hansen086964d2013-12-10 11:33:28 +000022
Jose Pascoal288dd2f2015-02-20 19:19:04 +000023import com.fairphone.updater.tools.Utils;
Jose Pascoalac6a8462014-09-10 20:12:08 +010024import com.fairphone.updater.widgets.gapps.GoogleAppsInstallerWidget;
Kim Hansen086964d2013-12-10 11:33:28 +000025
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000026import java.io.File;
27
Jose Pascoal810950b2014-10-09 17:16:08 +010028public class GappsInstallerHelper
29{
Jose Pascoal810950b2014-10-09 17:16:08 +010030 public static final String PREFS_GOOGLE_APPS_INSTALLER_DATA = "FAIRPHONE_GOOGLE_APPS_INSTALLER_DATA";
31 public static final String GOOGLE_APPS_INSTALLER_STATE = "com.fairphone.updater.gapps.WIDGET_STATE";
Kim Hansen086964d2013-12-10 11:33:28 +000032
Jose Pascoal810950b2014-10-09 17:16:08 +010033 public static final int GAPPS_STATES_INITIAL = 0;
Jose Pascoaleb0a1ed2014-12-16 16:23:08 +000034 public static final int GAPPS_INSTALLED_STATE = 1;
Kim Hansen086964d2013-12-10 11:33:28 +000035
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000036 public static final String EXTRA_START_GAPPS_INSTALL = "com.fairphone.updater.gapps.EXTRA_START_GAPPS_INSTALL";
Kim Hansen086964d2013-12-10 11:33:28 +000037
Jose Pascoaleb0a1ed2014-12-16 16:23:08 +000038 public static boolean areGappsInstalled()
39 {
Jose Pascoal288dd2f2015-02-20 19:19:04 +000040 return Utils.fileExists("/system/app/OneTimeInitializer.apk");
Jose Pascoal810950b2014-10-09 17:16:08 +010041 }
Jose Pascoal6143b112014-04-24 12:35:12 +010042
Jose Pascoal40916302015-02-06 18:43:47 +000043 public static void checkGappsAreInstalled(Context context)
Jose Pascoal810950b2014-10-09 17:16:08 +010044 {
Tiago Costa87925fe2014-12-02 17:57:51 +000045 if (areGappsInstalled())
Jose Pascoalda015b12014-11-06 12:47:11 +000046 {
Jose Pascoal40916302015-02-06 18:43:47 +000047 updateWidgetState(context, GAPPS_INSTALLED_STATE);
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000048 return;
Jose Pascoalda015b12014-11-06 12:47:11 +000049 }
50
Jose Pascoal40916302015-02-06 18:43:47 +000051 updateWidgetState(context, GAPPS_STATES_INITIAL);
Jose Pascoaleb0a1ed2014-12-16 16:23:08 +000052
Jose Pascoal810950b2014-10-09 17:16:08 +010053 }
Kim Hansen086964d2013-12-10 11:33:28 +000054
Jose Pascoal40916302015-02-06 18:43:47 +000055 private static void updateGoogleAppsIntallerWidgets(Context context)
Jose Pascoal810950b2014-10-09 17:16:08 +010056 {
Jose Pascoal40916302015-02-06 18:43:47 +000057 AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
58 int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, GoogleAppsInstallerWidget.class));
Jose Pascoal810950b2014-10-09 17:16:08 +010059 if (appWidgetIds.length > 0)
60 {
Jose Pascoal40916302015-02-06 18:43:47 +000061 new GoogleAppsInstallerWidget().onUpdate(context, appWidgetManager, appWidgetIds);
Jose Pascoal810950b2014-10-09 17:16:08 +010062 }
63 }
Kim Hansen086964d2013-12-10 11:33:28 +000064
Jose Pascoal40916302015-02-06 18:43:47 +000065 private static void updateInstallerState(Context context, int state)
Jose Pascoal810950b2014-10-09 17:16:08 +010066 {
Jose Pascoal40916302015-02-06 18:43:47 +000067 SharedPreferences sharedPrefs = context.getSharedPreferences(PREFS_GOOGLE_APPS_INSTALLER_DATA, Context.MODE_PRIVATE);
Jose Pascoal810950b2014-10-09 17:16:08 +010068 // alter State
Jose Pascoal40916302015-02-06 18:43:47 +000069 SharedPreferences.Editor prefEdit = sharedPrefs.edit();
Jose Pascoal810950b2014-10-09 17:16:08 +010070 prefEdit.putInt(GOOGLE_APPS_INSTALLER_STATE, state);
Jose Pascoal0b48f8d2015-02-06 16:06:41 +000071 prefEdit.apply();
Jose Pascoal810950b2014-10-09 17:16:08 +010072 }
Kim Hansen086964d2013-12-10 11:33:28 +000073
Jose Pascoal40916302015-02-06 18:43:47 +000074 private static void updateWidgetState(Context context, int state)
Jose Pascoal810950b2014-10-09 17:16:08 +010075 {
Jose Pascoal40916302015-02-06 18:43:47 +000076 updateInstallerState(context, state);
Kim Hansen086964d2013-12-10 11:33:28 +000077
Jose Pascoal40916302015-02-06 18:43:47 +000078 updateGoogleAppsIntallerWidgets(context);
Jose Pascoal810950b2014-10-09 17:16:08 +010079 }
Kim Hansen086964d2013-12-10 11:33:28 +000080}