New OneTimeInitializer to do upgrade steps.
Add upgrade step for phone launcher.
Bug: 6993891
Change-Id: I9599c21ccdeb7ccec2dbbd89ea720e636a48ca98
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..2ae437a
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,30 @@
+#
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(call all-java-files-under,src)
+
+LOCAL_STATIC_JAVA_LIBRARIES += android-common
+
+LOCAL_PACKAGE_NAME := OneTimeInitializer
+
+LOCAL_PROGUARD_FLAG_FILES := proguard.flags
+
+include $(BUILD_PACKAGE)
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
new file mode 100644
index 0000000..2ed8f59
--- /dev/null
+++ b/AndroidManifest.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<manifest
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.onetimeinitializer">
+
+ <uses-permission
+ android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
+ <uses-permission
+ android:name="com.android.launcher.permission.READ_SETTINGS"/>
+ <uses-permission
+ android:name="com.android.launcher.permission.WRITE_SETTINGS"/>
+
+ <application android:label="One Time Init">
+ <receiver
+ android:name=".OneTimeInitializerReceiver">
+ <intent-filter>
+ <action
+ android:name="android.intent.action.BOOT_COMPLETED"/>
+ </intent-filter>
+ </receiver>
+
+ <service
+ android:name=".OneTimeInitializerService">
+ </service>
+ </application>
+</manifest>
diff --git a/proguard.flags b/proguard.flags
new file mode 100644
index 0000000..6bdaadb
--- /dev/null
+++ b/proguard.flags
@@ -0,0 +1,4 @@
+-keep public class com.android.onetimeinitializer.OneTimeService
+-keep public class com.android.onetimeinitializer.OneTimeInitializerReceiver
+
+-verbose
diff --git a/src/com/android/onetimeinitializer/OneTimeInitializerReceiver.java b/src/com/android/onetimeinitializer/OneTimeInitializerReceiver.java
new file mode 100644
index 0000000..0f99a2b
--- /dev/null
+++ b/src/com/android/onetimeinitializer/OneTimeInitializerReceiver.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.onetimeinitializer;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+/**
+ * BroadcastReceiver that starts the service to performs one time initialization
+ * at bootup time.
+ */
+public class OneTimeInitializerReceiver extends BroadcastReceiver {
+
+ private static final String TAG = OneTimeInitializerReceiver.class.getSimpleName()
+ .substring(0, 22);
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Log.v(TAG, "OneTimeInitializerReceiver.onReceive");
+ context.startService(new Intent(context, OneTimeInitializerService.class));
+ }
+}
diff --git a/src/com/android/onetimeinitializer/OneTimeInitializerService.java b/src/com/android/onetimeinitializer/OneTimeInitializerService.java
new file mode 100644
index 0000000..cf72eae
--- /dev/null
+++ b/src/com/android/onetimeinitializer/OneTimeInitializerService.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.onetimeinitializer;
+
+import android.app.IntentService;
+import android.content.ComponentName;
+import android.content.ContentResolver;
+import android.content.ContentValues;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.database.Cursor;
+import android.net.Uri;
+import android.util.Log;
+
+import java.net.URISyntaxException;
+import java.util.Set;
+
+/**
+ * A class that performs one-time initialization after installation.
+ *
+ * <p>Android doesn't offer any mechanism to trigger an app right after installation, so we use the
+ * BOOT_COMPLETED broadcast intent instead. This means, when the app is upgraded, the
+ * initialization code here won't run until the device reboots.
+ */
+public class OneTimeInitializerService extends IntentService {
+
+ // class name is too long
+ private static final String TAG = OneTimeInitializerService.class.getSimpleName()
+ .substring(0, 22);
+
+ // Name of the shared preferences file.
+ private static final String SHARED_PREFS_FILE = "oti";
+
+ // Name of the preference containing the mapping version.
+ private static final String MAPPING_VERSION_PREF = "mapping_version";
+
+ // This is the content uri for Launcher content provider. See
+ // LauncherSettings and LauncherProvider in the Launcher app for details.
+ private static final Uri LAUNCHER_CONTENT_URI =
+ Uri.parse("content://com.android.launcher2.settings/favorites?notify=true");
+
+ private static final String LAUNCHER_ID_COLUMN = "_id";
+ private static final String LAUNCHER_INTENT_COLUMN = "intent";
+
+ private SharedPreferences mPreferences;
+
+ public OneTimeInitializerService() {
+ super("OneTimeInitializer Service");
+ }
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ mPreferences = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
+ }
+
+ @Override
+ protected void onHandleIntent(Intent intent) {
+ if (Log.isLoggable(TAG, Log.VERBOSE)) {
+ Log.v(TAG, "OneTimeInitializerService.onHandleIntent");
+ }
+
+ final int currentVersion = getMappingVersion();
+ int newVersion = currentVersion;
+ if (currentVersion < 1) {
+ if (Log.isLoggable(TAG, Log.INFO)) {
+ Log.i(TAG, "Updating to version 1.");
+ }
+ updateDialtactsLauncher();
+
+ newVersion = 1;
+ }
+
+ updateMappingVersion(newVersion);
+ }
+
+ private int getMappingVersion() {
+ return mPreferences.getInt(MAPPING_VERSION_PREF, 0);
+ }
+
+ private void updateMappingVersion(int version) {
+ SharedPreferences.Editor ed = mPreferences.edit();
+ ed.putInt(MAPPING_VERSION_PREF, version);
+ ed.commit();
+ }
+
+ private void updateDialtactsLauncher() {
+ ContentResolver cr = getContentResolver();
+ Cursor c = cr.query(LAUNCHER_CONTENT_URI,
+ new String[]{LAUNCHER_ID_COLUMN, LAUNCHER_INTENT_COLUMN}, null, null, null);
+ if (c == null) {
+ return;
+ }
+
+ try {
+ if (Log.isLoggable(TAG, Log.DEBUG)) {
+ Log.d(TAG, "Total launcher icons: " + c.getCount());
+ }
+
+ while (c.moveToNext()) {
+ long favoriteId = c.getLong(0);
+ final String intentUri = c.getString(1);
+ if (intentUri != null) {
+ try {
+ final Intent intent = Intent.parseUri(intentUri, 0);
+ final ComponentName componentName = intent.getComponent();
+ final Set<String> categories = intent.getCategories();
+ if (Intent.ACTION_MAIN.equals(intent.getAction()) &&
+ componentName != null &&
+ "com.android.contacts".equals(componentName.getPackageName()) &&
+ "com.android.contacts.activities.DialtactsActivity".equals(
+ componentName.getClassName()) &&
+ categories != null &&
+ categories.contains(Intent.CATEGORY_LAUNCHER)) {
+
+ final ComponentName newName = new ComponentName("com.android.dialer",
+ "com.android.dialer.DialtactsActivity");
+ intent.setComponent(newName);
+ final ContentValues values = new ContentValues();
+ values.put(LAUNCHER_INTENT_COLUMN, intent.toUri(0));
+
+ String updateWhere = LAUNCHER_ID_COLUMN + "=" + favoriteId;
+ cr.update(LAUNCHER_CONTENT_URI, values, updateWhere, null);
+ if (Log.isLoggable(TAG, Log.INFO)) {
+ Log.i(TAG, "Updated " + componentName + " to " + newName);
+ }
+ }
+ } catch (RuntimeException ex) {
+ Log.e(TAG, "Problem moving Dialtacts activity", ex);
+ } catch (URISyntaxException e) {
+ Log.e(TAG, "Problem moving Dialtacts activity", e);
+ }
+ }
+ }
+
+ } finally {
+ if (c != null) {
+ c.close();
+ }
+ }
+ }
+}