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