Utils.java revision 1eb3f316bd20c5d3e816876229c05bcf9f0c712c
1/** 2 * Copyright (C) 2007 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations 14 * under the License. 15 */ 16 17package com.android.settings; 18 19import android.app.ActivityManager; 20import android.app.AlertDialog; 21import android.app.Dialog; 22import android.app.Fragment; 23import android.app.IActivityManager; 24import android.content.ContentResolver; 25import android.content.Context; 26import android.content.DialogInterface; 27import android.content.Intent; 28import android.content.pm.ApplicationInfo; 29import android.content.pm.PackageManager; 30import android.content.pm.PackageManager.NameNotFoundException; 31import android.content.pm.ResolveInfo; 32import android.content.pm.UserInfo; 33import android.content.res.Resources; 34import android.content.res.Resources.NotFoundException; 35import android.database.Cursor; 36import android.graphics.Bitmap; 37import android.graphics.BitmapFactory; 38import android.graphics.drawable.Drawable; 39import android.net.ConnectivityManager; 40import android.net.LinkProperties; 41import android.net.Uri; 42import android.os.BatteryManager; 43import android.os.Bundle; 44import android.os.IBinder; 45import android.os.RemoteException; 46import android.os.UserHandle; 47import android.os.UserManager; 48import android.preference.Preference; 49import android.preference.PreferenceFrameLayout; 50import android.preference.PreferenceGroup; 51import android.provider.ContactsContract.CommonDataKinds; 52import android.provider.ContactsContract.Contacts; 53import android.provider.ContactsContract.Data; 54import android.provider.ContactsContract.Profile; 55import android.provider.ContactsContract.RawContacts; 56import android.telephony.TelephonyManager; 57import android.text.TextUtils; 58import android.util.Log; 59import android.view.View; 60import android.view.ViewGroup; 61import android.widget.ListView; 62import android.widget.TabWidget; 63 64import com.android.settings.dashboard.DashboardCategory; 65import com.android.settings.dashboard.DashboardTile; 66 67import java.io.IOException; 68import java.io.InputStream; 69import java.net.InetAddress; 70import java.util.Iterator; 71import java.util.List; 72import java.util.Locale; 73 74public final class Utils { 75 private static final String TAG = "Settings"; 76 /** 77 * Set the preference's title to the matching activity's label. 78 */ 79 public static final int UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY = 1; 80 81 /** 82 * The opacity level of a disabled icon. 83 */ 84 public static final float DISABLED_ALPHA = 0.4f; 85 86 /** 87 * Color spectrum to use to indicate badness. 0 is completely transparent (no data), 88 * 1 is most bad (red), the last value is least bad (green). 89 */ 90 public static final int[] BADNESS_COLORS = new int[] { 91 0x00000000, 0xffc43828, 0xffe54918, 0xfff47b00, 92 0xfffabf2c, 0xff679e37, 0xff0a7f42 93 }; 94 95 /** 96 * Name of the meta-data item that should be set in the AndroidManifest.xml 97 * to specify the icon that should be displayed for the preference. 98 */ 99 private static final String META_DATA_PREFERENCE_ICON = "com.android.settings.icon"; 100 101 /** 102 * Name of the meta-data item that should be set in the AndroidManifest.xml 103 * to specify the title that should be displayed for the preference. 104 */ 105 private static final String META_DATA_PREFERENCE_TITLE = "com.android.settings.title"; 106 107 /** 108 * Name of the meta-data item that should be set in the AndroidManifest.xml 109 * to specify the summary text that should be displayed for the preference. 110 */ 111 private static final String META_DATA_PREFERENCE_SUMMARY = "com.android.settings.summary"; 112 113 /** 114 * Finds a matching activity for a preference's intent. If a matching 115 * activity is not found, it will remove the preference. 116 * 117 * @param context The context. 118 * @param parentPreferenceGroup The preference group that contains the 119 * preference whose intent is being resolved. 120 * @param preferenceKey The key of the preference whose intent is being 121 * resolved. 122 * @param flags 0 or one or more of 123 * {@link #UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY} 124 * . 125 * @return Whether an activity was found. If false, the preference was 126 * removed. 127 */ 128 public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, 129 PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { 130 131 Preference preference = parentPreferenceGroup.findPreference(preferenceKey); 132 if (preference == null) { 133 return false; 134 } 135 136 Intent intent = preference.getIntent(); 137 if (intent != null) { 138 // Find the activity that is in the system image 139 PackageManager pm = context.getPackageManager(); 140 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); 141 int listSize = list.size(); 142 for (int i = 0; i < listSize; i++) { 143 ResolveInfo resolveInfo = list.get(i); 144 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) 145 != 0) { 146 147 // Replace the intent with this specific activity 148 preference.setIntent(new Intent().setClassName( 149 resolveInfo.activityInfo.packageName, 150 resolveInfo.activityInfo.name)); 151 152 if ((flags & UPDATE_PREFERENCE_FLAG_SET_TITLE_TO_MATCHING_ACTIVITY) != 0) { 153 // Set the preference title to the activity's label 154 preference.setTitle(resolveInfo.loadLabel(pm)); 155 } 156 157 return true; 158 } 159 } 160 } 161 162 // Did not find a matching activity, so remove the preference 163 parentPreferenceGroup.removePreference(preference); 164 165 return false; 166 } 167 168 public static boolean updateTileToSpecificActivityFromMetaDataOrRemove(Context context, 169 DashboardCategory target, DashboardTile tile) { 170 171 Intent intent = tile.intent; 172 if (intent != null) { 173 // Find the activity that is in the system image 174 PackageManager pm = context.getPackageManager(); 175 List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA); 176 int listSize = list.size(); 177 for (int i = 0; i < listSize; i++) { 178 ResolveInfo resolveInfo = list.get(i); 179 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) 180 != 0) { 181 Drawable icon = null; 182 String title = null; 183 String summary = null; 184 185 // Get the activity's meta-data 186 try { 187 Resources res = pm.getResourcesForApplication( 188 resolveInfo.activityInfo.packageName); 189 Bundle metaData = resolveInfo.activityInfo.metaData; 190 191 if (res != null && metaData != null) { 192 icon = res.getDrawable(metaData.getInt(META_DATA_PREFERENCE_ICON)); 193 title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE)); 194 summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY)); 195 } 196 } catch (NameNotFoundException e) { 197 // Ignore 198 } catch (NotFoundException e) { 199 // Ignore 200 } 201 202 // Set the preference title to the activity's label if no 203 // meta-data is found 204 if (TextUtils.isEmpty(title)) { 205 title = resolveInfo.loadLabel(pm).toString(); 206 } 207 208 // Set icon, title and summary for the preference 209 // TODO: 210 //tile.icon = icon; 211 tile.title = title; 212 tile.summary = summary; 213 // Replace the intent with this specific activity 214 tile.intent = new Intent().setClassName(resolveInfo.activityInfo.packageName, 215 resolveInfo.activityInfo.name); 216 217 return true; 218 } 219 } 220 } 221 222 // Did not find a matching activity, so remove the preference 223 target.removeTile(tile); 224 225 return false; 226 } 227 228 /** 229 * Returns true if Monkey is running. 230 */ 231 public static boolean isMonkeyRunning() { 232 return ActivityManager.isUserAMonkey(); 233 } 234 235 /** 236 * Returns whether the device is voice-capable (meaning, it is also a phone). 237 */ 238 public static boolean isVoiceCapable(Context context) { 239 TelephonyManager telephony = 240 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 241 return telephony != null && telephony.isVoiceCapable(); 242 } 243 244 public static boolean isWifiOnly(Context context) { 245 ConnectivityManager cm = (ConnectivityManager)context.getSystemService( 246 Context.CONNECTIVITY_SERVICE); 247 return (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false); 248 } 249 250 /** 251 * Returns the WIFI IP Addresses, if any, taking into account IPv4 and IPv6 style addresses. 252 * @param context the application context 253 * @return the formatted and newline-separated IP addresses, or null if none. 254 */ 255 public static String getWifiIpAddresses(Context context) { 256 ConnectivityManager cm = (ConnectivityManager) 257 context.getSystemService(Context.CONNECTIVITY_SERVICE); 258 LinkProperties prop = cm.getLinkProperties(ConnectivityManager.TYPE_WIFI); 259 return formatIpAddresses(prop); 260 } 261 262 /** 263 * Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style 264 * addresses. 265 * @param context the application context 266 * @return the formatted and newline-separated IP addresses, or null if none. 267 */ 268 public static String getDefaultIpAddresses(ConnectivityManager cm) { 269 LinkProperties prop = cm.getActiveLinkProperties(); 270 return formatIpAddresses(prop); 271 } 272 273 private static String formatIpAddresses(LinkProperties prop) { 274 if (prop == null) return null; 275 Iterator<InetAddress> iter = prop.getAllAddresses().iterator(); 276 // If there are no entries, return null 277 if (!iter.hasNext()) return null; 278 // Concatenate all available addresses, comma separated 279 String addresses = ""; 280 while (iter.hasNext()) { 281 addresses += iter.next().getHostAddress(); 282 if (iter.hasNext()) addresses += "\n"; 283 } 284 return addresses; 285 } 286 287 public static Locale createLocaleFromString(String localeStr) { 288 // TODO: is there a better way to actually construct a locale that will match? 289 // The main problem is, on top of Java specs, locale.toString() and 290 // new Locale(locale.toString()).toString() do not return equal() strings in 291 // many cases, because the constructor takes the only string as the language 292 // code. So : new Locale("en", "US").toString() => "en_US" 293 // And : new Locale("en_US").toString() => "en_us" 294 if (null == localeStr) 295 return Locale.getDefault(); 296 String[] brokenDownLocale = localeStr.split("_", 3); 297 // split may not return a 0-length array. 298 if (1 == brokenDownLocale.length) { 299 return new Locale(brokenDownLocale[0]); 300 } else if (2 == brokenDownLocale.length) { 301 return new Locale(brokenDownLocale[0], brokenDownLocale[1]); 302 } else { 303 return new Locale(brokenDownLocale[0], brokenDownLocale[1], brokenDownLocale[2]); 304 } 305 } 306 307 public static boolean isBatteryPresent(Intent batteryChangedIntent) { 308 return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); 309 } 310 311 public static String getBatteryPercentage(Intent batteryChangedIntent) { 312 return String.valueOf(getBatteryLevel(batteryChangedIntent)) + "%"; 313 } 314 315 public static int getBatteryLevel(Intent batteryChangedIntent) { 316 int level = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); 317 int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 100); 318 return (level * 100) / scale; 319 } 320 321 public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) { 322 final Intent intent = batteryChangedIntent; 323 324 int plugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); 325 int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 326 BatteryManager.BATTERY_STATUS_UNKNOWN); 327 String statusString; 328 if (status == BatteryManager.BATTERY_STATUS_CHARGING) { 329 int resId; 330 if (plugType == BatteryManager.BATTERY_PLUGGED_AC) { 331 resId = R.string.battery_info_status_charging_ac; 332 } else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) { 333 resId = R.string.battery_info_status_charging_usb; 334 } else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) { 335 resId = R.string.battery_info_status_charging_wireless; 336 } else { 337 resId = R.string.battery_info_status_charging; 338 } 339 statusString = res.getString(resId); 340 } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { 341 statusString = res.getString(R.string.battery_info_status_discharging); 342 } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { 343 statusString = res.getString(R.string.battery_info_status_not_charging); 344 } else if (status == BatteryManager.BATTERY_STATUS_FULL) { 345 statusString = res.getString(R.string.battery_info_status_full); 346 } else { 347 statusString = res.getString(R.string.battery_info_status_unknown); 348 } 349 350 return statusString; 351 } 352 353 public static void forcePrepareCustomPreferencesList( 354 ViewGroup parent, View child, ListView list, boolean ignoreSidePadding) { 355 list.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); 356 list.setClipToPadding(false); 357 prepareCustomPreferencesList(parent, child, list, ignoreSidePadding); 358 } 359 360 /** 361 * Prepare a custom preferences layout, moving padding to {@link ListView} 362 * when outside scrollbars are requested. Usually used to display 363 * {@link ListView} and {@link TabWidget} with correct padding. 364 */ 365 public static void prepareCustomPreferencesList( 366 ViewGroup parent, View child, View list, boolean ignoreSidePadding) { 367 final boolean movePadding = list.getScrollBarStyle() == View.SCROLLBARS_OUTSIDE_OVERLAY; 368 if (movePadding && parent instanceof PreferenceFrameLayout) { 369 ((PreferenceFrameLayout.LayoutParams) child.getLayoutParams()).removeBorders = true; 370 371 final Resources res = list.getResources(); 372 final int paddingSide = res.getDimensionPixelSize(R.dimen.settings_side_margin); 373 final int paddingBottom = res.getDimensionPixelSize( 374 com.android.internal.R.dimen.preference_fragment_padding_bottom); 375 376 final int effectivePaddingSide = ignoreSidePadding ? 0 : paddingSide; 377 list.setPaddingRelative(effectivePaddingSide, 0, effectivePaddingSide, paddingBottom); 378 } 379 } 380 381 /** 382 * Return string resource that best describes combination of tethering 383 * options available on this device. 384 */ 385 public static int getTetheringLabel(ConnectivityManager cm) { 386 String[] usbRegexs = cm.getTetherableUsbRegexs(); 387 String[] wifiRegexs = cm.getTetherableWifiRegexs(); 388 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs(); 389 390 boolean usbAvailable = usbRegexs.length != 0; 391 boolean wifiAvailable = wifiRegexs.length != 0; 392 boolean bluetoothAvailable = bluetoothRegexs.length != 0; 393 394 if (wifiAvailable && usbAvailable && bluetoothAvailable) { 395 return R.string.tether_settings_title_all; 396 } else if (wifiAvailable && usbAvailable) { 397 return R.string.tether_settings_title_all; 398 } else if (wifiAvailable && bluetoothAvailable) { 399 return R.string.tether_settings_title_all; 400 } else if (wifiAvailable) { 401 return R.string.tether_settings_title_wifi; 402 } else if (usbAvailable && bluetoothAvailable) { 403 return R.string.tether_settings_title_usb_bluetooth; 404 } else if (usbAvailable) { 405 return R.string.tether_settings_title_usb; 406 } else { 407 return R.string.tether_settings_title_bluetooth; 408 } 409 } 410 411 /* Used by UserSettings as well. Call this on a non-ui thread. */ 412 public static boolean copyMeProfilePhoto(Context context, UserInfo user) { 413 Uri contactUri = Profile.CONTENT_URI; 414 415 InputStream avatarDataStream = Contacts.openContactPhotoInputStream( 416 context.getContentResolver(), 417 contactUri, true); 418 // If there's no profile photo, assign a default avatar 419 if (avatarDataStream == null) { 420 return false; 421 } 422 int userId = user != null ? user.id : UserHandle.myUserId(); 423 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); 424 Bitmap icon = BitmapFactory.decodeStream(avatarDataStream); 425 um.setUserIcon(userId, icon); 426 try { 427 avatarDataStream.close(); 428 } catch (IOException ioe) { } 429 return true; 430 } 431 432 public static String getMeProfileName(Context context, boolean full) { 433 if (full) { 434 return getProfileDisplayName(context); 435 } else { 436 return getShorterNameIfPossible(context); 437 } 438 } 439 440 private static String getShorterNameIfPossible(Context context) { 441 final String given = getLocalProfileGivenName(context); 442 return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context); 443 } 444 445 private static String getLocalProfileGivenName(Context context) { 446 final ContentResolver cr = context.getContentResolver(); 447 448 // Find the raw contact ID for the local ME profile raw contact. 449 final long localRowProfileId; 450 final Cursor localRawProfile = cr.query( 451 Profile.CONTENT_RAW_CONTACTS_URI, 452 new String[] {RawContacts._ID}, 453 RawContacts.ACCOUNT_TYPE + " IS NULL AND " + 454 RawContacts.ACCOUNT_NAME + " IS NULL", 455 null, null); 456 if (localRawProfile == null) return null; 457 458 try { 459 if (!localRawProfile.moveToFirst()) { 460 return null; 461 } 462 localRowProfileId = localRawProfile.getLong(0); 463 } finally { 464 localRawProfile.close(); 465 } 466 467 // Find the structured name for the raw contact. 468 final Cursor structuredName = cr.query( 469 Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(), 470 new String[] {CommonDataKinds.StructuredName.GIVEN_NAME, 471 CommonDataKinds.StructuredName.FAMILY_NAME}, 472 Data.RAW_CONTACT_ID + "=" + localRowProfileId, 473 null, null); 474 if (structuredName == null) return null; 475 476 try { 477 if (!structuredName.moveToFirst()) { 478 return null; 479 } 480 String partialName = structuredName.getString(0); 481 if (TextUtils.isEmpty(partialName)) { 482 partialName = structuredName.getString(1); 483 } 484 return partialName; 485 } finally { 486 structuredName.close(); 487 } 488 } 489 490 private static final String getProfileDisplayName(Context context) { 491 final ContentResolver cr = context.getContentResolver(); 492 final Cursor profile = cr.query(Profile.CONTENT_URI, 493 new String[] {Profile.DISPLAY_NAME}, null, null, null); 494 if (profile == null) return null; 495 496 try { 497 if (!profile.moveToFirst()) { 498 return null; 499 } 500 return profile.getString(0); 501 } finally { 502 profile.close(); 503 } 504 } 505 506 /** Not global warming, it's global change warning. */ 507 public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId, 508 final Runnable positiveAction) { 509 final AlertDialog.Builder builder = new AlertDialog.Builder(context); 510 builder.setTitle(titleResId); 511 builder.setMessage(R.string.global_change_warning); 512 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 513 @Override 514 public void onClick(DialogInterface dialog, int which) { 515 positiveAction.run(); 516 } 517 }); 518 builder.setNegativeButton(android.R.string.cancel, null); 519 520 return builder.create(); 521 } 522 523 public static boolean hasMultipleUsers(Context context) { 524 return ((UserManager) context.getSystemService(Context.USER_SERVICE)) 525 .getUsers().size() > 1; 526 } 527 528 /** 529 * Start a new instance of the activity, showing only the given fragment. 530 * When launched in this mode, the given preference fragment will be instantiated and fill the 531 * entire activity. 532 * 533 * @param context The context. 534 * @param fragmentName The name of the fragment to display. 535 * @param args Optional arguments to supply to the fragment. 536 * @param resultTo Option fragment that should receive the result of the activity launch. 537 * @param resultRequestCode If resultTo is non-null, this is the request code in which 538 * to report the result. 539 * @param titleResId resource id for the String to display for the title of this set 540 * of preferences. 541 * @param title String to display for the title of this set of preferences. 542 */ 543 public static void startWithFragment(Context context, String fragmentName, Bundle args, 544 Fragment resultTo, int resultRequestCode, int titleResId, CharSequence title) { 545 startWithFragment(context, fragmentName, args, resultTo, resultRequestCode, 546 titleResId, title, false /* not a shortcut */); 547 } 548 549 public static void startWithFragment(Context context, String fragmentName, Bundle args, 550 Fragment resultTo, int resultRequestCode, int titleResId, CharSequence title, 551 boolean isShortcut) { 552 Intent intent = onBuildStartFragmentIntent(context, fragmentName, args, titleResId, 553 title, isShortcut); 554 if (resultTo == null) { 555 context.startActivity(intent); 556 } else { 557 resultTo.startActivityForResult(intent, resultRequestCode); 558 } 559 } 560 561 /** 562 * Build an Intent to launch a new activity showing the selected fragment. 563 * The implementation constructs an Intent that re-launches the current activity with the 564 * appropriate arguments to display the fragment. 565 * 566 * 567 * @param context The Context. 568 * @param fragmentName The name of the fragment to display. 569 * @param args Optional arguments to supply to the fragment. 570 * @param titleResId Optional title resource id to show for this item. 571 * @param title Optional title to show for this item. 572 * @param isShortcut tell if this is a Launcher Shortcut or not 573 * @return Returns an Intent that can be launched to display the given 574 * fragment. 575 */ 576 public static Intent onBuildStartFragmentIntent(Context context, String fragmentName, 577 Bundle args, int titleResId, CharSequence title, boolean isShortcut) { 578 Intent intent = new Intent(Intent.ACTION_MAIN); 579 intent.setClass(context, SubSettings.class); 580 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, fragmentName); 581 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS, args); 582 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE_RESID, titleResId); 583 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_TITLE, title); 584 intent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_AS_SHORTCUT, isShortcut); 585 return intent; 586 } 587 588 /** 589 * Returns the managed profile of the current user or null if none found. 590 */ 591 public static UserHandle getManagedProfile(UserManager userManager) { 592 List<UserHandle> userProfiles = userManager.getUserProfiles(); 593 final int count = userProfiles.size(); 594 for (int i = 0; i < count; i++) { 595 final UserHandle profile = userProfiles.get(i); 596 if (profile.getIdentifier() == userManager.getUserHandle()) { 597 continue; 598 } 599 final UserInfo userInfo = userManager.getUserInfo(profile.getIdentifier()); 600 if (userInfo.isManagedProfile()) { 601 return profile; 602 } 603 } 604 return null; 605 } 606 607 /** 608 * Returns true if the current profile is a managed one. 609 */ 610 public static boolean isManagedProfile(UserManager userManager) { 611 UserInfo currentUser = userManager.getUserInfo(userManager.getUserHandle()); 612 return currentUser.isManagedProfile(); 613 } 614 615 /** 616 * Returns the {@link UserHandle} of the profile that a settings screen should refer to. 617 * 618 * <p> This takes into account the id of the user that triggered the settings screen. 619 */ 620 public static UserHandle getProfileToDisplay(IActivityManager am, IBinder activityToken, 621 Bundle arguments) { 622 int currentUser = UserHandle.getCallingUserId(); 623 // Check to see if it was called from a different user 624 try { 625 int launchedFromUser = UserHandle.getUserId(am.getLaunchedFromUid(activityToken)); 626 if (launchedFromUser != currentUser) { 627 // This is a forwarded intent 628 return new UserHandle(launchedFromUser); 629 } 630 } catch (RemoteException e) { 631 // Should not happen 632 Log.v(TAG, "Could not get launching user."); 633 } 634 // TODO: Check fragment arguments. See: http://b/15466880 635 636 // Default to current profile 637 return new UserHandle(currentUser); 638 } 639} 640