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.example.android.apis.app; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.os.Parcelable; 23 import android.widget.TextView; 24 25 import com.example.android.apis.R; 26 27 /** 28 * This Activity actually handles two stages of a launcher shortcut's life cycle. 29 * 30 * 1. Your application offers to provide shortcuts to the launcher. When 31 * the user installs a shortcut, an activity within your application 32 * generates the actual shortcut and returns it to the launcher, where it 33 * is shown to the user as an icon. 34 * 35 * 2. Any time the user clicks on an installed shortcut, an intent is sent. 36 * Typically this would then be handled as necessary by an activity within 37 * your application. 38 * 39 * We handle stage 1 (creating a shortcut) by simply sending back the information (in the form 40 * of an {@link android.content.Intent} that the launcher will use to create the shortcut. 41 * 42 * You can also implement this in an interactive way, by having your activity actually present 43 * UI for the user to select the specific nature of the shortcut, such as a contact, picture, URL, 44 * media item, or action. 45 * 46 * We handle stage 2 (responding to a shortcut) in this sample by simply displaying the contents 47 * of the incoming {@link android.content.Intent}. 48 * 49 * In a real application, you would probably use the shortcut intent to display specific content 50 * or start a particular operation. 51 */ 52 public class LauncherShortcuts extends Activity { 53 54 private static final String EXTRA_KEY = "com.example.android.apis.app.LauncherShortcuts"; 55 56 @Override onCreate(Bundle icicle)57 public void onCreate(Bundle icicle) { 58 super.onCreate(icicle); 59 60 // Resolve the intent 61 62 final Intent intent = getIntent(); 63 final String action = intent.getAction(); 64 65 // If the intent is a request to create a shortcut, we'll do that and exit 66 67 if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) { 68 setupShortcut(); 69 finish(); 70 return; 71 } 72 73 // If we weren't launched with a CREATE_SHORTCUT intent, simply put up an informative 74 // display. 75 76 // Inflate our UI from its XML layout description. 77 78 setContentView(R.layout.launcher_shortcuts); 79 80 // Provide a lightweight view of the Intent that launched us 81 82 TextView intentInfo = (TextView) findViewById(R.id.txt_shortcut_intent); 83 String info = intent.toString(); 84 String extra = intent.getStringExtra(EXTRA_KEY); 85 if (extra != null) { 86 info = info + " " + extra; 87 } 88 intentInfo.setText(info); 89 } 90 91 /** 92 * This function creates a shortcut and returns it to the caller. There are actually two 93 * intents that you will send back. 94 * 95 * The first intent serves as a container for the shortcut and is returned to the launcher by 96 * setResult(). This intent must contain three fields: 97 * 98 * <ul> 99 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_INTENT} The shortcut intent.</li> 100 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_NAME} The text that will be displayed with 101 * the shortcut.</li> 102 * <li>{@link android.content.Intent#EXTRA_SHORTCUT_ICON} The shortcut's icon, if provided as a 103 * bitmap, <i>or</i> {@link android.content.Intent#EXTRA_SHORTCUT_ICON_RESOURCE} if provided as 104 * a drawable resource.</li> 105 * </ul> 106 * 107 * If you use a simple drawable resource, note that you must wrapper it using 108 * {@link android.content.Intent.ShortcutIconResource}, as shown below. This is required so 109 * that the launcher can access resources that are stored in your application's .apk file. If 110 * you return a bitmap, such as a thumbnail, you can simply put the bitmap into the extras 111 * bundle using {@link android.content.Intent#EXTRA_SHORTCUT_ICON}. 112 * 113 * The shortcut intent can be any intent that you wish the launcher to send, when the user 114 * clicks on the shortcut. Typically this will be {@link android.content.Intent#ACTION_VIEW} 115 * with an appropriate Uri for your content, but any Intent will work here as long as it 116 * triggers the desired action within your Activity. 117 */ setupShortcut()118 private void setupShortcut() { 119 // First, set up the shortcut intent. For this example, we simply create an intent that 120 // will bring us directly back to this activity. A more typical implementation would use a 121 // data Uri in order to display a more specific result, or a custom action in order to 122 // launch a specific operation. 123 124 Intent shortcutIntent = new Intent(Intent.ACTION_MAIN); 125 shortcutIntent.setClassName(this, this.getClass().getName()); 126 shortcutIntent.putExtra(EXTRA_KEY, "ApiDemos Provided This Shortcut"); 127 128 // Then, set up the container intent (the response to the caller) 129 130 Intent intent = new Intent(); 131 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 132 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcut_name)); 133 Parcelable iconResource = Intent.ShortcutIconResource.fromContext( 134 this, R.drawable.app_sample_code); 135 intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource); 136 137 // Now, return the result to the launcher 138 139 setResult(RESULT_OK, intent); 140 } 141 } 142