IntentCompat.java revision f19cb1f3facdb9f90ef1b26537ff303eaa3db77d
1/* 2 * Copyright (C) 2011 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 17package android.support.v4.content; 18 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.os.Build; 23 24/** 25 * Helper for accessing features in {@link android.content.Intent} 26 * introduced after API level 4 in a backwards compatible fashion. 27 */ 28public class IntentCompat { 29 30 interface IntentCompatImpl { 31 Intent makeMainActivity(ComponentName componentName); 32 } 33 34 static class IntentCompatImplBase implements IntentCompatImpl { 35 @Override 36 public Intent makeMainActivity(ComponentName componentName) { 37 Intent intent = new Intent(Intent.ACTION_MAIN); 38 intent.setComponent(componentName); 39 intent.addCategory(Intent.CATEGORY_LAUNCHER); 40 return intent; 41 } 42 } 43 44 static class IntentCompatImplHC implements IntentCompatImpl { 45 @Override 46 public Intent makeMainActivity(ComponentName componentName) { 47 return IntentCompatHoneycomb.makeMainActivity(componentName); 48 } 49 } 50 51 private static final IntentCompatImpl IMPL; 52 static { 53 if (Build.VERSION.SDK_INT >= 11) { 54 IMPL = new IntentCompatImplHC(); 55 } else { 56 IMPL = new IntentCompatImplBase(); 57 } 58 } 59 60 private IntentCompat() { 61 /* Hide constructor */ 62 } 63 64 /** 65 * Broadcast Action: Resources for a set of packages (which were 66 * previously unavailable) are currently 67 * available since the media on which they exist is available. 68 * The extra data {@link #EXTRA_CHANGED_PACKAGE_LIST} contains a 69 * list of packages whose availability changed. 70 * The extra data {@link #EXTRA_CHANGED_UID_LIST} contains a 71 * list of uids of packages whose availability changed. 72 * Note that the 73 * packages in this list do <em>not</em> receive this broadcast. 74 * The specified set of packages are now available on the system. 75 * <p>Includes the following extras: 76 * <ul> 77 * <li> {@link #EXTRA_CHANGED_PACKAGE_LIST} is the set of packages 78 * whose resources(were previously unavailable) are currently available. 79 * {@link #EXTRA_CHANGED_UID_LIST} is the set of uids of the 80 * packages whose resources(were previously unavailable) 81 * are currently available. 82 * </ul> 83 * 84 * <p class="note">This is a protected intent that can only be sent 85 * by the system. 86 */ 87 public static final String ACTION_EXTERNAL_APPLICATIONS_AVAILABLE = 88 "android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE"; 89 90 /** 91 * Broadcast Action: Resources for a set of packages are currently 92 * unavailable since the media on which they exist is unavailable. 93 * The extra data {@link #EXTRA_CHANGED_PACKAGE_LIST} contains a 94 * list of packages whose availability changed. 95 * The extra data {@link #EXTRA_CHANGED_UID_LIST} contains a 96 * list of uids of packages whose availability changed. 97 * The specified set of packages can no longer be 98 * launched and are practically unavailable on the system. 99 * <p>Inclues the following extras: 100 * <ul> 101 * <li> {@link #EXTRA_CHANGED_PACKAGE_LIST} is the set of packages 102 * whose resources are no longer available. 103 * {@link #EXTRA_CHANGED_UID_LIST} is the set of packages 104 * whose resources are no longer available. 105 * </ul> 106 * 107 * <p class="note">This is a protected intent that can only be sent 108 * by the system. 109 */ 110 public static final String ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE = 111 "android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE"; 112 113 /** 114 * This field is part of 115 * {@link android.content.Intent#ACTION_EXTERNAL_APPLICATIONS_AVAILABLE}, 116 * {@link android.content.Intent#ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE} 117 * and contains a string array of all of the components that have changed. 118 */ 119 public static final String EXTRA_CHANGED_PACKAGE_LIST = 120 "android.intent.extra.changed_package_list"; 121 122 /** 123 * This field is part of 124 * {@link android.content.Intent#ACTION_EXTERNAL_APPLICATIONS_AVAILABLE}, 125 * {@link android.content.Intent#ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE} 126 * and contains an integer array of uids of all of the components 127 * that have changed. 128 */ 129 public static final String EXTRA_CHANGED_UID_LIST = 130 "android.intent.extra.changed_uid_list"; 131 132 /** 133 * A constant String that is associated with the Intent, used with 134 * {@link android.content.Intent#ACTION_SEND} to supply an alternative to 135 * {@link android.content.Intent#EXTRA_TEXT} 136 * as HTML formatted text. Note that you <em>must</em> also supply 137 * {@link android.content.Intent#EXTRA_TEXT}. 138 */ 139 public static final String EXTRA_HTML_TEXT = "android.intent.extra.HTML_TEXT"; 140 141 /** 142 * If set in an Intent passed to {@link Context#startActivity Context.startActivity()}, 143 * this flag will cause a newly launching task to be placed on top of the current 144 * home activity task (if there is one). That is, pressing back from the task 145 * will always return the user to home even if that was not the last activity they 146 * saw. This can only be used in conjunction with 147 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK}. 148 */ 149 public static final int FLAG_ACTIVITY_TASK_ON_HOME = 0x00004000; 150 151 /** 152 * If set in an Intent passed to {@link Context#startActivity Context.startActivity()}, 153 * this flag will cause any existing task that would be associated with the 154 * activity to be cleared before the activity is started. That is, the activity 155 * becomes the new root of an otherwise empty task, and any old activities 156 * are finished. This can only be used in conjunction with 157 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK}. 158 * 159 * <p>This flag will only be obeyed on devices supporting API 11 or higher.</p> 160 */ 161 public static final int FLAG_ACTIVITY_CLEAR_TASK = 0x00008000; 162 163 /** 164 * Create an intent to launch the main (root) activity of a task. This 165 * is the Intent that is started when the application's is launched from 166 * Home. For anything else that wants to launch an application in the 167 * same way, it is important that they use an Intent structured the same 168 * way, and can use this function to ensure this is the case. 169 * 170 * <p>The returned Intent has the given Activity component as its explicit 171 * component, {@link Intent#ACTION_MAIN ACTION_MAIN} as its action, and includes the 172 * category {@link Intent#CATEGORY_LAUNCHER CATEGORY_LAUNCHER}. This does <em>not</em> have 173 * {@link Intent#FLAG_ACTIVITY_NEW_TASK FLAG_ACTIVITY_NEW_TASK} set, 174 * though typically you will want to do that through {@link Intent#addFlags(int) addFlags(int)} 175 * on the returned Intent. 176 * 177 * @param mainActivity The main activity component that this Intent will 178 * launch. 179 * @return Returns a newly created Intent that can be used to launch the 180 * activity as a main application entry. 181 * 182 * @see Intent#setClass 183 * @see Intent#setComponent 184 */ 185 public static Intent makeMainActivity(ComponentName mainActivity) { 186 return IMPL.makeMainActivity(mainActivity); 187 } 188} 189