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 | */ |
Jose Pascoal | ac6a846 | 2014-09-10 20:12:08 +0100 | [diff] [blame] | 16 | package com.fairphone.updater.gappsinstaller; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 17 | |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 18 | import android.appwidget.AppWidgetManager; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 21 | import android.content.SharedPreferences; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 22 | |
Jose Pascoal | 288dd2f | 2015-02-20 19:19:04 +0000 | [diff] [blame] | 23 | import com.fairphone.updater.tools.Utils; |
Jose Pascoal | ac6a846 | 2014-09-10 20:12:08 +0100 | [diff] [blame] | 24 | import com.fairphone.updater.widgets.gapps.GoogleAppsInstallerWidget; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 25 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 26 | import java.io.File; |
| 27 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 28 | public class GappsInstallerHelper |
| 29 | { |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 30 | 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 Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 32 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 33 | public static final int GAPPS_STATES_INITIAL = 0; |
Jose Pascoal | eb0a1ed | 2014-12-16 16:23:08 +0000 | [diff] [blame] | 34 | public static final int GAPPS_INSTALLED_STATE = 1; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 35 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 36 | public static final String EXTRA_START_GAPPS_INSTALL = "com.fairphone.updater.gapps.EXTRA_START_GAPPS_INSTALL"; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 37 | |
Jose Pascoal | eb0a1ed | 2014-12-16 16:23:08 +0000 | [diff] [blame] | 38 | public static boolean areGappsInstalled() |
| 39 | { |
Jose Pascoal | 288dd2f | 2015-02-20 19:19:04 +0000 | [diff] [blame] | 40 | return Utils.fileExists("/system/app/OneTimeInitializer.apk"); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 41 | } |
Jose Pascoal | 6143b11 | 2014-04-24 12:35:12 +0100 | [diff] [blame] | 42 | |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 43 | public static void checkGappsAreInstalled(Context context) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 44 | { |
Tiago Costa | 87925fe | 2014-12-02 17:57:51 +0000 | [diff] [blame] | 45 | if (areGappsInstalled()) |
Jose Pascoal | da015b1 | 2014-11-06 12:47:11 +0000 | [diff] [blame] | 46 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 47 | updateWidgetState(context, GAPPS_INSTALLED_STATE); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 48 | return; |
Jose Pascoal | da015b1 | 2014-11-06 12:47:11 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 51 | updateWidgetState(context, GAPPS_STATES_INITIAL); |
Jose Pascoal | eb0a1ed | 2014-12-16 16:23:08 +0000 | [diff] [blame] | 52 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 53 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 54 | |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 55 | private static void updateGoogleAppsIntallerWidgets(Context context) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 56 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 57 | AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); |
| 58 | int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, GoogleAppsInstallerWidget.class)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 59 | if (appWidgetIds.length > 0) |
| 60 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 61 | new GoogleAppsInstallerWidget().onUpdate(context, appWidgetManager, appWidgetIds); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 62 | } |
| 63 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 64 | |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 65 | private static void updateInstallerState(Context context, int state) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 66 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 67 | SharedPreferences sharedPrefs = context.getSharedPreferences(PREFS_GOOGLE_APPS_INSTALLER_DATA, Context.MODE_PRIVATE); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 68 | // alter State |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 69 | SharedPreferences.Editor prefEdit = sharedPrefs.edit(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 70 | prefEdit.putInt(GOOGLE_APPS_INSTALLER_STATE, state); |
Jose Pascoal | 0b48f8d | 2015-02-06 16:06:41 +0000 | [diff] [blame] | 71 | prefEdit.apply(); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 72 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 73 | |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 74 | private static void updateWidgetState(Context context, int state) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 75 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 76 | updateInstallerState(context, state); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 77 | |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 78 | updateGoogleAppsIntallerWidgets(context); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 79 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 80 | } |