• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source 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 
17 package com.android.onetimeinitializer;
18 
19 import android.app.IntentService;
20 import android.content.ComponentName;
21 import android.content.ContentResolver;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.SharedPreferences;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.util.Log;
29 
30 import java.net.URISyntaxException;
31 import java.util.Set;
32 
33 /**
34  * A class that performs one-time initialization after installation.
35  *
36  * <p>Android doesn't offer any mechanism to trigger an app right after installation, so we use the
37  * BOOT_COMPLETED broadcast intent instead.  This means, when the app is upgraded, the
38  * initialization code here won't run until the device reboots.
39  */
40 public class OneTimeInitializerService extends IntentService {
41 
42     // class name is too long
43     private static final String TAG = OneTimeInitializerService.class.getSimpleName()
44             .substring(0, 22);
45 
46     // Name of the shared preferences file.
47     private static final String SHARED_PREFS_FILE = "oti";
48 
49     // Name of the preference containing the mapping version.
50     private static final String MAPPING_VERSION_PREF = "mapping_version";
51 
52     // This is the content uri for Launcher content provider. See
53     // LauncherSettings and LauncherProvider in the Launcher app for details.
54     private static final Uri LAUNCHER_CONTENT_URI =
55             Uri.parse("content://com.android.launcher2.settings/favorites?notify=true");
56 
57     private static final String LAUNCHER_ID_COLUMN = "_id";
58     private static final String LAUNCHER_INTENT_COLUMN = "intent";
59 
60     private SharedPreferences mPreferences;
61 
OneTimeInitializerService()62     public OneTimeInitializerService() {
63         super("OneTimeInitializer Service");
64     }
65 
66     @Override
onCreate()67     public void onCreate() {
68         super.onCreate();
69         mPreferences = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
70     }
71 
72     @Override
onHandleIntent(Intent intent)73     protected void onHandleIntent(Intent intent) {
74         if (Log.isLoggable(TAG, Log.VERBOSE)) {
75             Log.v(TAG, "OneTimeInitializerService.onHandleIntent");
76         }
77 
78         final int currentVersion = getMappingVersion();
79         int newVersion = currentVersion;
80         if (currentVersion < 1) {
81             if (Log.isLoggable(TAG, Log.INFO)) {
82                 Log.i(TAG, "Updating to version 1.");
83             }
84             updateDialtactsLauncher();
85 
86             newVersion = 1;
87         }
88 
89         updateMappingVersion(newVersion);
90     }
91 
getMappingVersion()92     private int getMappingVersion() {
93         return mPreferences.getInt(MAPPING_VERSION_PREF, 0);
94     }
95 
updateMappingVersion(int version)96     private void updateMappingVersion(int version) {
97         SharedPreferences.Editor ed = mPreferences.edit();
98         ed.putInt(MAPPING_VERSION_PREF, version);
99         ed.commit();
100     }
101 
updateDialtactsLauncher()102     private void updateDialtactsLauncher() {
103         ContentResolver cr = getContentResolver();
104         Cursor c = cr.query(LAUNCHER_CONTENT_URI,
105                 new String[]{LAUNCHER_ID_COLUMN, LAUNCHER_INTENT_COLUMN}, null, null, null);
106         if (c == null) {
107             return;
108         }
109 
110         try {
111             if (Log.isLoggable(TAG, Log.DEBUG)) {
112                 Log.d(TAG, "Total launcher icons: " + c.getCount());
113             }
114 
115             while (c.moveToNext()) {
116                 long favoriteId = c.getLong(0);
117                 final String intentUri = c.getString(1);
118                 if (intentUri != null) {
119                     try {
120                         final Intent intent = Intent.parseUri(intentUri, 0);
121                         final ComponentName componentName = intent.getComponent();
122                         final Set<String> categories = intent.getCategories();
123                         if (Intent.ACTION_MAIN.equals(intent.getAction()) &&
124                                 componentName != null &&
125                                 "com.android.contacts".equals(componentName.getPackageName()) &&
126                                 "com.android.contacts.activities.DialtactsActivity".equals(
127                                         componentName.getClassName()) &&
128                                 categories != null &&
129                                 categories.contains(Intent.CATEGORY_LAUNCHER)) {
130 
131                             final ComponentName newName = new ComponentName("com.android.dialer",
132                                     "com.android.dialer.DialtactsActivity");
133                             intent.setComponent(newName);
134                             final ContentValues values = new ContentValues();
135                             values.put(LAUNCHER_INTENT_COLUMN, intent.toUri(0));
136 
137                             String updateWhere = LAUNCHER_ID_COLUMN + "=" + favoriteId;
138                             cr.update(LAUNCHER_CONTENT_URI, values, updateWhere, null);
139                             if (Log.isLoggable(TAG, Log.INFO)) {
140                                 Log.i(TAG, "Updated " + componentName + " to " + newName);
141                             }
142                         }
143                     } catch (RuntimeException ex) {
144                         Log.e(TAG, "Problem moving Dialtacts activity", ex);
145                     } catch (URISyntaxException e) {
146                         Log.e(TAG, "Problem moving Dialtacts activity", e);
147                     }
148                 }
149             }
150 
151         } finally {
152             if (c != null) {
153                 c.close();
154             }
155         }
156     }
157 }
158