• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.launcher2;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.ContentResolver;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.widget.Toast;
26 
27 import java.net.URISyntaxException;
28 
29 public class UninstallShortcutReceiver extends BroadcastReceiver {
30     private static final String ACTION_UNINSTALL_SHORTCUT =
31             "com.android.launcher.action.UNINSTALL_SHORTCUT";
32 
onReceive(Context context, Intent data)33     public void onReceive(Context context, Intent data) {
34         if (!ACTION_UNINSTALL_SHORTCUT.equals(data.getAction())) {
35             return;
36         }
37 
38         Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
39         String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
40         boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true);
41 
42         if (intent != null && name != null) {
43             final ContentResolver cr = context.getContentResolver();
44             Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI,
45                 new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT },
46                 LauncherSettings.Favorites.TITLE + "=?", new String[] { name }, null);
47 
48             final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
49             final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
50 
51             boolean changed = false;
52 
53             try {
54                 while (c.moveToNext()) {
55                     try {
56                         if (intent.filterEquals(Intent.parseUri(c.getString(intentIndex), 0))) {
57                             final long id = c.getLong(idIndex);
58                             final Uri uri = LauncherSettings.Favorites.getContentUri(id, false);
59                             cr.delete(uri, null, null);
60                             changed = true;
61                             if (!duplicate) {
62                                 break;
63                             }
64                         }
65                     } catch (URISyntaxException e) {
66                         // Ignore
67                     }
68                 }
69             } finally {
70                 c.close();
71             }
72 
73             if (changed) {
74                 cr.notifyChange(LauncherSettings.Favorites.CONTENT_URI, null);
75                 Toast.makeText(context, context.getString(R.string.shortcut_uninstalled, name),
76                         Toast.LENGTH_SHORT).show();
77             }
78         }
79     }
80 }
81