summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2017-04-10 10:49:01 -0700
committerSunny Goyal <sunnygoyal@google.com>2017-05-03 11:24:54 -0700
commit3e443a24576be93c8ecdeba29b04aa07b165d718 (patch)
tree57e31cfc552333521ead7a10b5364e5acdf4b201
parent70cbd2cdb9748bc2e050a325804f7f4d5f89afc1 (diff)
downloadandroid_packages_apps_Trebuchet-3e443a24576be93c8ecdeba29b04aa07b165d718.tar.gz
android_packages_apps_Trebuchet-3e443a24576be93c8ecdeba29b04aa07b165d718.tar.bz2
android_packages_apps_Trebuchet-3e443a24576be93c8ecdeba29b04aa07b165d718.zip
Initializing the Add To Homescreen setting by reading it from market app
> Also enabling add to home-to-homescreen feature based on install reason Bug: 36588249 Change-Id: Icfc1edd4c31ed7eb50086f8ffb1a7858a9641b41
-rw-r--r--src/com/android/launcher3/LauncherProvider.java1
-rw-r--r--src/com/android/launcher3/SessionCommitReceiver.java102
2 files changed, 85 insertions, 18 deletions
diff --git a/src/com/android/launcher3/LauncherProvider.java b/src/com/android/launcher3/LauncherProvider.java
index 7d85ac1f9..d4e3171e5 100644
--- a/src/com/android/launcher3/LauncherProvider.java
+++ b/src/com/android/launcher3/LauncherProvider.java
@@ -124,6 +124,7 @@ public class LauncherProvider extends ContentProvider {
// always available in the main process.
FileLog.setDir(getContext().getApplicationContext().getFilesDir());
IconShapeOverride.apply(getContext());
+ SessionCommitReceiver.applyDefaultUserPrefs(getContext());
return true;
}
diff --git a/src/com/android/launcher3/SessionCommitReceiver.java b/src/com/android/launcher3/SessionCommitReceiver.java
index 203bc2523..61bcc178c 100644
--- a/src/com/android/launcher3/SessionCommitReceiver.java
+++ b/src/com/android/launcher3/SessionCommitReceiver.java
@@ -16,6 +16,7 @@
package com.android.launcher3;
+import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -23,39 +24,51 @@ import android.content.SharedPreferences;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.PackageInstaller;
import android.content.pm.PackageInstaller.SessionInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.AsyncTask;
+import android.os.Build;
import android.os.Process;
import android.os.UserHandle;
+import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import com.android.launcher3.compat.LauncherAppsCompat;
-import com.android.launcher3.compat.UserManagerCompat;
import java.util.List;
/**
* BroadcastReceiver to handle session commit intent.
*/
+@TargetApi(Build.VERSION_CODES.O)
public class SessionCommitReceiver extends BroadcastReceiver {
- private static final long SESSION_IGNORE_DURATION = 3 * 60 * 60 * 1000; // 3 hours
+ private static final String TAG = "SessionCommitReceiver";
+
+ // The content provider for the add to home screen setting. It should be of the format:
+ // <package name>.addtohomescreen
+ private static final String MARKER_PROVIDER_PREFIX = ".addtohomescreen";
// Preference key for automatically adding icon to homescreen.
public static final String ADD_ICON_PREFERENCE_KEY = "pref_add_icon_to_home";
-
- private static final String KEY_FIRST_TIME = "first_session_broadcast_time";
+ public static final String ADD_ICON_PREFERENCE_INITIALIZED_KEY =
+ "pref_add_icon_to_home_initialized";
@Override
public void onReceive(Context context, Intent intent) {
- if (!isEnabled(context)) {
+ if (!isEnabled(context) || !Utilities.isAtLeastO()) {
// User has decided to not add icons on homescreen.
return;
}
SessionInfo info = intent.getParcelableExtra(PackageInstaller.EXTRA_SESSION);
UserHandle user = intent.getParcelableExtra(Intent.EXTRA_USER);
- // TODO: Verify install reason
- if (TextUtils.isEmpty(info.getAppPackageName())) {
+
+ if (TextUtils.isEmpty(info.getAppPackageName()) ||
+ info.getInstallReason() != PackageManager.INSTALL_REASON_USER) {
return;
}
@@ -64,17 +77,6 @@ public class SessionCommitReceiver extends BroadcastReceiver {
return;
}
- // STOPSHIP: Remove this workaround when we start getting proper install reason
- SharedPreferences prefs = context
- .getSharedPreferences(LauncherFiles.DEVICE_PREFERENCES_KEY, 0);
- long now = System.currentTimeMillis();
- long firstTime = prefs.getLong(KEY_FIRST_TIME, now);
- prefs.edit().putLong(KEY_FIRST_TIME, firstTime).apply();
- if ((now - firstTime) < SESSION_IGNORE_DURATION) {
- Log.d("SessionCommitReceiver", "Temporarily ignoring session broadcast");
- return;
- }
-
List<LauncherActivityInfo> activities = LauncherAppsCompat.getInstance(context)
.getActivityList(info.getAppPackageName(), user);
if (activities == null || activities.isEmpty()) {
@@ -87,4 +89,68 @@ public class SessionCommitReceiver extends BroadcastReceiver {
public static boolean isEnabled(Context context) {
return Utilities.getPrefs(context).getBoolean(ADD_ICON_PREFERENCE_KEY, true);
}
+
+ public static void applyDefaultUserPrefs(final Context context) {
+ if (!Utilities.isAtLeastO()) {
+ return;
+ }
+ SharedPreferences prefs = Utilities.getPrefs(context);
+ if (prefs.getAll().isEmpty()) {
+ // This logic assumes that the code is the first thing that is executed (before any
+ // shared preference is written).
+ // TODO: Move this logic to DB upgrade once we have proper support for db downgrade
+ // If it is a fresh start, just apply the default value. We use prefs.isEmpty() to infer
+ // a fresh start as put preferences always contain some values corresponding to current
+ // grid.
+ prefs.edit().putBoolean(ADD_ICON_PREFERENCE_KEY, true).apply();
+ } else if (!prefs.contains(ADD_ICON_PREFERENCE_INITIALIZED_KEY)) {
+ new PrefInitTask(context).executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
+ }
+ }
+
+ private static class PrefInitTask extends AsyncTask<Void, Void, Void> {
+ private final Context mContext;
+
+ PrefInitTask(Context context) {
+ mContext = context;
+ }
+
+ @Override
+ protected Void doInBackground(Void... voids) {
+ boolean addIconToHomeScreenEnabled = readValueFromMarketApp();
+ Utilities.getPrefs(mContext).edit()
+ .putBoolean(ADD_ICON_PREFERENCE_KEY, addIconToHomeScreenEnabled)
+ .putBoolean(ADD_ICON_PREFERENCE_INITIALIZED_KEY, true)
+ .apply();
+ return null;
+ }
+
+ public boolean readValueFromMarketApp() {
+ // Get the marget package
+ ResolveInfo ri = mContext.getPackageManager().resolveActivity(
+ new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET),
+ PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_SYSTEM_ONLY);
+ if (ri == null) {
+ return true;
+ }
+
+ Cursor c = null;
+ try {
+ c = mContext.getContentResolver().query(
+ Uri.parse("content://" + ri.activityInfo.packageName
+ + MARKER_PROVIDER_PREFIX),
+ null, null, null, null);
+ if (c.moveToNext()) {
+ return c.getInt(c.getColumnIndexOrThrow(Settings.NameValueTable.VALUE)) != 0;
+ }
+ } catch (Exception e) {
+ Log.d(TAG, "Error reading add to homescreen preference", e);
+ } finally {
+ if (c != null) {
+ c.close();
+ }
+ }
+ return true;
+ }
+ }
}