1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*
* Copyright (C) 2010 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.camera;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import java.util.ArrayList;
/**
* 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.
*
* <p>This is adapted from the Email app.
*/
public class OneTimeInitializer extends BroadcastReceiver {
private static final String TAG = "camera";
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "OneTimeInitializer.onReceive");
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
initialize(context);
}
}
/**
* Perform the one-time initialization.
*/
private void initialize(Context context) {
// Disable itself.
setComponentEnabled(context, getClass(), false);
// If in the future we have more than one thing to do, we may also
// store the progress in a preference like the Email app does.
updateShortcut(context);
}
private void setComponentEnabled(Context context, Class<?> clazz, boolean enabled) {
final ComponentName c = new ComponentName(context, clazz.getName());
context.getPackageManager().setComponentEnabledSetting(c,
enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
// 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 class Entry {
ComponentName mOldComp, mNewComp;
Entry(ComponentName oldComp, ComponentName newComp) {
mOldComp = oldComp;
mNewComp = newComp;
}
}
private ArrayList<Entry> mappingTable;
private void prepareTable(Context context) {
mappingTable = new ArrayList<Entry>();
// We have two names for each of the old component.
String oldPkg = "com.android.camera";
String newPkg = context.getPackageName();
Log.v(TAG, "oldPkg = " + oldPkg + ", newPkg = " + newPkg);
ComponentName oldCamera = new ComponentName(
oldPkg, "com.android.camera.Camera");
ComponentName oldCameraShort = new ComponentName(
oldPkg, ".Camera");
ComponentName oldVideoCamera = new ComponentName(
oldPkg, "com.android.camera.VideoCamera");
ComponentName oldVideoCameraShort = new ComponentName(
oldPkg, ".VideoCamera");
// The new names.
ComponentName newCamera = new ComponentName(
newPkg, "com.android.camera.Camera");
ComponentName newVideoCamera = new ComponentName(
newPkg, "com.android.camera.VideoCamera");
mappingTable.add(new Entry(oldCamera, newCamera));
mappingTable.add(new Entry(oldCameraShort, newCamera));
mappingTable.add(new Entry(oldVideoCamera, newVideoCamera));
mappingTable.add(new Entry(oldVideoCameraShort, newVideoCamera));
}
private void updateShortcut(Context context) {
prepareTable(context);
ContentResolver cr = context.getContentResolver();
Cursor c = cr.query(LAUNCHER_CONTENT_URI,
new String[] { "_id", "intent" }, null, null, null);
if (c == null) return;
ContentValues values = new ContentValues(1);
try {
while (c.moveToNext()) {
try {
long id = c.getLong(0);
String intentUri = c.getString(1);
if (intentUri == null) continue;
Intent shortcut = Intent.parseUri(intentUri, 0);
ComponentName comp = shortcut.getComponent();
for (Entry e : mappingTable) {
if (comp.equals(e.mOldComp)) {
Log.v(TAG, "fix shortcut id " + id + " from " +
comp + " to " + e.mNewComp);
shortcut.setComponent(e.mNewComp);
values.put("intent", shortcut.toUri(0));
cr.update(LAUNCHER_CONTENT_URI, values, "_id = ?",
new String[] { String.valueOf(id) });
break;
}
}
} catch (Throwable t) {
// Move to the next one if there is any problem.
Log.w(TAG, "can't parse a shortcut entry", t);
continue;
}
}
} finally {
c.close();
}
}
}
|