Example usage for android.app ActivityManagerNative getDefault

List of usage examples for android.app ActivityManagerNative getDefault

Introduction

In this page you can find the example usage for android.app ActivityManagerNative getDefault.

Prototype

@UnsupportedAppUsage
static public IActivityManager getDefault() 

Source Link

Document

Retrieve the system's default/global activity manager.

Usage

From source file:com.android.managedprovisioning.ProfileOwnerProvisioningService.java

/**
 * Initialize the user that underlies the managed profile.
 * This is required so that the provisioning complete broadcast can be sent across to the
 * profile and apps can run on it./*from   w ww.  java 2s  .  co m*/
 */
private boolean startManagedProfile(int userId) {
    ProvisionLogger.logd("Starting user in background");
    IActivityManager iActivityManager = ActivityManagerNative.getDefault();
    // Register a receiver for the Intent.ACTION_USER_UNLOCKED to know when the managed profile
    // has been started and unlocked.
    mUnlockedReceiver = new UserUnlockedReceiver(this, userId);
    try {
        return iActivityManager.startUserInBackground(userId);
    } catch (RemoteException neverThrown) {
        // Never thrown, as we are making local calls.
        ProvisionLogger.loge("This should not happen.", neverThrown);
    }
    return false;
}

From source file:android.content.BroadcastReceiver.java

/**
 * Provide a binder to an already-running service.  This method is synchronous
 * and will not start the target service if it is not present, so it is safe
 * to call from {@link #onReceive}.//from  w  w  w.  jav a2s.c  om
 * 
 * @param myContext The Context that had been passed to {@link #onReceive(Context, Intent)}
 * @param service The Intent indicating the service you wish to use.  See {@link
 * Context#startService(Intent)} for more information.
 */
public IBinder peekService(Context myContext, Intent service) {
    IActivityManager am = ActivityManagerNative.getDefault();
    IBinder binder = null;
    try {
        service.setAllowFds(false);
        binder = am.peekService(service, service.resolveTypeIfNeeded(myContext.getContentResolver()));
    } catch (RemoteException e) {
    }
    return binder;
}

From source file:com.android.packageinstaller.PackageInstallerActivity.java

/** Get the originating uid if possible, or VerificationParams.NO_UID if not available */
private int getOriginatingUid(Intent intent) {
    // The originating uid from the intent. We only trust/use this if it comes from a
    // system application
    int uidFromIntent = intent.getIntExtra(Intent.EXTRA_ORIGINATING_UID, VerificationParams.NO_UID);

    // Get the source info from the calling package, if available. This will be the
    // definitive calling package, but it only works if the intent was started using
    // startActivityForResult,
    ApplicationInfo sourceInfo = getSourceInfo();
    if (sourceInfo != null) {
        if (uidFromIntent != VerificationParams.NO_UID
                && (mSourceInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
            return uidFromIntent;

        }//from  ww w .j  a  v  a  2  s .  c o  m
        // We either didn't get a uid in the intent, or we don't trust it. Use the
        // uid of the calling package instead.
        return sourceInfo.uid;
    }

    // We couldn't get the specific calling package. Let's get the uid instead
    int callingUid;
    try {
        callingUid = ActivityManagerNative.getDefault().getLaunchedFromUid(getActivityToken());
    } catch (android.os.RemoteException ex) {
        Log.w(TAG, "Could not determine the launching uid.");
        // nothing else we can do
        return VerificationParams.NO_UID;
    }

    // If we got a uid from the intent, we need to verify that the caller is a
    // privileged system package before we use it
    if (uidFromIntent != VerificationParams.NO_UID) {
        String[] callingPackages = mPm.getPackagesForUid(callingUid);
        if (callingPackages != null) {
            for (String packageName : callingPackages) {
                try {
                    ApplicationInfo applicationInfo = mPm.getApplicationInfo(packageName, 0);

                    if ((applicationInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0) {
                        return uidFromIntent;
                    }
                } catch (NameNotFoundException ex) {
                    // ignore it, and try the next package
                }
            }
        }
    }
    // We either didn't get a uid from the intent, or we don't trust it. Use the
    // calling uid instead.
    return callingUid;
}

From source file:com.android.server.MountService.java

private void copyLocaleFromMountService() {
    String systemLocale;//from  w w w.  j  a  v a2  s.c om
    try {
        systemLocale = getField(StorageManager.SYSTEM_LOCALE_KEY);
    } catch (RemoteException e) {
        return;
    }
    if (TextUtils.isEmpty(systemLocale)) {
        return;
    }

    Slog.d(TAG, "Got locale " + systemLocale + " from mount service");
    Locale locale = Locale.forLanguageTag(systemLocale);
    Configuration config = new Configuration();
    config.setLocale(locale);
    try {
        ActivityManagerNative.getDefault().updateConfiguration(config);
    } catch (RemoteException e) {
        Slog.e(TAG, "Error setting system locale from mount service", e);
    }

    // Temporary workaround for http://b/17945169.
    Slog.d(TAG, "Setting system properties to " + systemLocale + " from mount service");
    SystemProperties.set("persist.sys.language", locale.getLanguage());
    SystemProperties.set("persist.sys.country", locale.getCountry());
}

From source file:android.app.Activity.java

/**
 * Report to the system that your app is now fully drawn, purely for diagnostic
 * purposes (calling it does not impact the visible behavior of the activity).
 * This is only used to help instrument application launch times, so that the
 * app can report when it is fully in a usable state; without this, the only thing
 * the system itself can determine is the point at which the activity's window
 * is <em>first</em> drawn and displayed.  To participate in app launch time
 * measurement, you should always call this method after first launch (when
 * {@link #onCreate(android.os.Bundle)} is called), at the point where you have
 * entirely drawn your UI and populated with all of the significant data.  You
 * can safely call this method any time after first launch as well, in which case
 * it will simply be ignored.//from  w w  w. j  a va 2s .  com
 */
public void reportFullyDrawn() {
    if (mDoReportFullyDrawn) {
        mDoReportFullyDrawn = false;
        try {
            ActivityManagerNative.getDefault().reportActivityFullyDrawn(mToken);
        } catch (RemoteException e) {
        }
    }
}

From source file:android.app.Activity.java

private void startIntentSenderForResultInner(IntentSender intent, int requestCode, Intent fillInIntent,
        int flagsMask, int flagsValues, Activity activity, Bundle options)
        throws IntentSender.SendIntentException {
    try {//from   w  w  w  . j a v a2 s . co  m
        String resolvedType = null;
        if (fillInIntent != null) {
            fillInIntent.migrateExtraStreamToClipData();
            fillInIntent.prepareToLeaveProcess();
            resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
        }
        int result = ActivityManagerNative.getDefault().startActivityIntentSender(
                mMainThread.getApplicationThread(), intent, fillInIntent, resolvedType, mToken,
                activity.mEmbeddedID, requestCode, flagsMask, flagsValues, options);
        if (result == ActivityManager.START_CANCELED) {
            throw new IntentSender.SendIntentException();
        }
        Instrumentation.checkStartActivityResult(result, null);
    } catch (RemoteException e) {
    }
    if (requestCode >= 0) {
        // If this start is requesting a result, we can avoid making
        // the activity visible until the result is received.  Setting
        // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
        // activity hidden during this time, to avoid flickering.
        // This can only be done when a result is requested because
        // that guarantees we will get information back when the
        // activity is finished, no matter what happens to it.
        mStartedActivity = true;
    }
}

From source file:android.app.Activity.java

/**
 * A special variation to launch an activity only if a new activity
 * instance is needed to handle the given Intent.  In other words, this is
 * just like {@link #startActivityForResult(Intent, int)} except: if you are 
 * using the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} flag, or
 * singleTask or singleTop /*from  w w w  .j av  a 2  s. c  om*/
 * {@link android.R.styleable#AndroidManifestActivity_launchMode launchMode},
 * and the activity 
 * that handles <var>intent</var> is the same as your currently running 
 * activity, then a new instance is not needed.  In this case, instead of 
 * the normal behavior of calling {@link #onNewIntent} this function will 
 * return and you can handle the Intent yourself. 
 * 
 * <p>This function can only be called from a top-level activity; if it is
 * called from a child activity, a runtime exception will be thrown.
 * 
 * @param intent The intent to start.
 * @param requestCode If >= 0, this code will be returned in
 *         onActivityResult() when the activity exits, as described in
 *         {@link #startActivityForResult}.
 * @param options Additional options for how the Activity should be started.
 * See {@link android.content.Context#startActivity(Intent, Bundle)
 * Context.startActivity(Intent, Bundle)} for more details.
 * 
 * @return If a new activity was launched then true is returned; otherwise
 *         false is returned and you must handle the Intent yourself.
 *  
 * @see #startActivity
 * @see #startActivityForResult
 */
public boolean startActivityIfNeeded(Intent intent, int requestCode, Bundle options) {
    if (mParent == null) {
        int result = ActivityManager.START_RETURN_INTENT_TO_CALLER;
        try {
            intent.migrateExtraStreamToClipData();
            intent.prepareToLeaveProcess();
            result = ActivityManagerNative.getDefault().startActivity(mMainThread.getApplicationThread(),
                    getBasePackageName(), intent, intent.resolveTypeIfNeeded(getContentResolver()), mToken,
                    mEmbeddedID, requestCode, ActivityManager.START_FLAG_ONLY_IF_NEEDED, null, null, options);
        } catch (RemoteException e) {
            // Empty
        }

        Instrumentation.checkStartActivityResult(result, intent);

        if (requestCode >= 0) {
            // If this start is requesting a result, we can avoid making
            // the activity visible until the result is received.  Setting
            // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
            // activity hidden during this time, to avoid flickering.
            // This can only be done when a result is requested because
            // that guarantees we will get information back when the
            // activity is finished, no matter what happens to it.
            mStartedActivity = true;
        }
        return result != ActivityManager.START_RETURN_INTENT_TO_CALLER;
    }

    throw new UnsupportedOperationException(
            "startActivityIfNeeded can only be called from a top-level activity");
}

From source file:android.app.Activity.java

/**
 * Special version of starting an activity, for use when you are replacing
 * other activity components.  You can use this to hand the Intent off
 * to the next Activity that can handle it.  You typically call this in
 * {@link #onCreate} with the Intent returned by {@link #getIntent}.
 * /*from   w w w  . jav  a2 s .  c  o m*/
 * @param intent The intent to dispatch to the next activity.  For
 * correct behavior, this must be the same as the Intent that started
 * your own activity; the only changes you can make are to the extras
 * inside of it.
 * @param options Additional options for how the Activity should be started.
 * See {@link android.content.Context#startActivity(Intent, Bundle)
 * Context.startActivity(Intent, Bundle)} for more details.
 * 
 * @return Returns a boolean indicating whether there was another Activity
 * to start: true if there was a next activity to start, false if there
 * wasn't.  In general, if true is returned you will then want to call
 * finish() on yourself.
 */
public boolean startNextMatchingActivity(Intent intent, Bundle options) {
    if (mParent == null) {
        try {
            intent.migrateExtraStreamToClipData();
            intent.prepareToLeaveProcess();
            return ActivityManagerNative.getDefault().startNextMatchingActivity(mToken, intent, options);
        } catch (RemoteException e) {
            // Empty
        }
        return false;
    }

    throw new UnsupportedOperationException(
            "startNextMatchingActivity can only be called from a top-level activity");
}

From source file:android.app.Activity.java

/**
 * Call immediately after one of the flavors of {@link #startActivity(Intent)}
 * or {@link #finish} to specify an explicit transition animation to
 * perform next.//w ww .j  a v  a  2  s.  c o  m
 *
 * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative
 * to using this with starting activities is to supply the desired animation
 * information through a {@link ActivityOptions} bundle to
 * {@link #startActivity(Intent, Bundle) or a related function.  This allows
 * you to specify a custom animation even when starting an activity from
 * outside the context of the current top activity.
 *
 * @param enterAnim A resource ID of the animation resource to use for
 * the incoming activity.  Use 0 for no animation.
 * @param exitAnim A resource ID of the animation resource to use for
 * the outgoing activity.  Use 0 for no animation.
 */
public void overridePendingTransition(int enterAnim, int exitAnim) {
    try {
        ActivityManagerNative.getDefault().overridePendingTransition(mToken, getPackageName(), enterAnim,
                exitAnim);
    } catch (RemoteException e) {
    }
}

From source file:android.app.Activity.java

/**
 * Return the name of the package that invoked this activity.  This is who
 * the data in {@link #setResult setResult()} will be sent to.  You can
 * use this information to validate that the recipient is allowed to
 * receive the data.//from   ww w. j a va 2  s  .c  o  m
 * 
 * <p class="note">Note: if the calling activity is not expecting a result (that is it
 * did not use the {@link #startActivityForResult} 
 * form that includes a request code), then the calling package will be 
 * null.</p>
 *
 * <p class="note">Note: prior to {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2},
 * the result from this method was unstable.  If the process hosting the calling
 * package was no longer running, it would return null instead of the proper package
 * name.  You can use {@link #getCallingActivity()} and retrieve the package name
 * from that instead.</p>
 * 
 * @return The package of the activity that will receive your
 *         reply, or null if none.
 */
public String getCallingPackage() {
    try {
        return ActivityManagerNative.getDefault().getCallingPackage(mToken);
    } catch (RemoteException e) {
        return null;
    }
}