Java Code Examples for android.app.Activity#isResumed()

The following examples show how to use android.app.Activity#isResumed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NfcFCardEmulation.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Disables the service for the specified Activity.
 *
 * <p>Note that the specified Activity must still be in resumed
 * state at the time of this call. A good place to call this method
 * is in your {@link Activity#onPause} implementation.
 *
 * @param activity The activity which the service was registered for
 * @return true when successful
 */
public boolean disableService(Activity activity) throws RuntimeException {
    if (activity == null) {
        throw new NullPointerException("activity is null");
    }
    if (!activity.isResumed()) {
        throw new IllegalArgumentException("Activity must be resumed.");
    }
    try {
        return sService.disableNfcFForegroundService();
    } catch (RemoteException e) {
        // Try one more time
        recoverService();
        if (sService == null) {
            Log.e(TAG, "Failed to recover CardEmulationService.");
            return false;
        }
        try {
            return sService.disableNfcFForegroundService();
        } catch (RemoteException ee) {
            Log.e(TAG, "Failed to reach CardEmulationService.");
            ee.rethrowAsRuntimeException();
            return false;
        }
    }
}
 
Example 2
Source File: CardEmulation.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Unsets the preferred service for the specified Activity.
 *
 * <p>Note that the specified Activity must still be in resumed
 * state at the time of this call. A good place to call this method
 * is in your {@link Activity#onPause} implementation.
 *
 * @param activity The activity which the service was registered for
 * @return true when successful
 */
public boolean unsetPreferredService(Activity activity) {
    if (activity == null) {
        throw new NullPointerException("activity is null");
    }
    if (!activity.isResumed()) {
        throw new IllegalArgumentException("Activity must be resumed.");
    }
    try {
        return sService.unsetPreferredService();
    } catch (RemoteException e) {
        // Try one more time
        recoverService();
        if (sService == null) {
            Log.e(TAG, "Failed to recover CardEmulationService.");
            return false;
        }
        try {
            return sService.unsetPreferredService();
        } catch (RemoteException ee) {
            Log.e(TAG, "Failed to reach CardEmulationService.");
            return false;
        }
    }
}
 
Example 3
Source File: NfcFCardEmulation.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Allows a foreground application to specify which card emulation service
 * should be enabled while a specific Activity is in the foreground.
 *
 * <p>The specified HCE-F service is only enabled when the corresponding application is
 * in the foreground and this method has been called. When the application is moved to
 * the background, {@link #disableService(Activity)} is called, or
 * NFCID2 or System Code is replaced, the HCE-F service is disabled.
 *
 * <p>The specified Activity must currently be in resumed state. A good
 * paradigm is to call this method in your {@link Activity#onResume}, and to call
 * {@link #disableService(Activity)} in your {@link Activity#onPause}.
 *
 * <p>Note that this preference is not persisted by the OS, and hence must be
 * called every time the Activity is resumed.
 *
 * @param activity The activity which prefers this service to be invoked
 * @param service The service to be preferred while this activity is in the foreground
 * @return whether the registration was successful
 */
public boolean enableService(Activity activity, ComponentName service) throws RuntimeException {
    if (activity == null || service == null) {
        throw new NullPointerException("activity or service is null");
    }
    // Verify the activity is in the foreground before calling into NfcService
    if (!activity.isResumed()) {
        throw new IllegalArgumentException("Activity must be resumed.");
    }
    try {
        return sService.enableNfcFForegroundService(service);
    } catch (RemoteException e) {
        // Try one more time
        recoverService();
        if (sService == null) {
            Log.e(TAG, "Failed to recover CardEmulationService.");
            return false;
        }
        try {
            return sService.enableNfcFForegroundService(service);
        } catch (RemoteException ee) {
            Log.e(TAG, "Failed to reach CardEmulationService.");
            ee.rethrowAsRuntimeException();
            return false;
        }
    }
}
 
Example 4
Source File: NfcActivityManager.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
public NfcActivityState(Activity activity) {
    if (activity.getWindow().isDestroyed()) {
        throw new IllegalStateException("activity is already destroyed");
    }
    // Check if activity is resumed right now, as we will not
    // immediately get a callback for that.
    resumed = activity.isResumed();

    this.activity = activity;
    this.token = new Binder();
    registerApplication(activity.getApplication());
}
 
Example 5
Source File: NfcAdapter.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
void disableForegroundDispatchInternal(Activity activity, boolean force) {
    try {
        sService.setForegroundDispatch(null, null, null);
        if (!force && !activity.isResumed()) {
            throw new IllegalStateException("You must disable foreground dispatching " +
                    "while your activity is still resumed");
        }
    } catch (RemoteException e) {
        attemptDeadServiceRecovery(e);
    }
}
 
Example 6
Source File: CardEmulation.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Allows a foreground application to specify which card emulation service
 * should be preferred while a specific Activity is in the foreground.
 *
 * <p>The specified Activity must currently be in resumed state. A good
 * paradigm is to call this method in your {@link Activity#onResume}, and to call
 * {@link #unsetPreferredService(Activity)} in your {@link Activity#onPause}.
 *
 * <p>This method call will fail in two specific scenarios:
 * <ul>
 * <li> If the service registers one or more AIDs in the {@link #CATEGORY_PAYMENT}
 * category, but the user has indicated that foreground apps are not allowed
 * to override the default payment service.
 * <li> If the service registers one or more AIDs in the {@link #CATEGORY_OTHER}
 * category that are also handled by the default payment service, and the
 * user has indicated that foreground apps are not allowed to override the
 * default payment service.
 * </ul>
 *
 * <p> Use {@link #categoryAllowsForegroundPreference(String)} to determine
 * whether foreground apps can override the default payment service.
 *
 * <p>Note that this preference is not persisted by the OS, and hence must be
 * called every time the Activity is resumed.
 *
 * @param activity The activity which prefers this service to be invoked
 * @param service The service to be preferred while this activity is in the foreground
 * @return whether the registration was successful
 */
public boolean setPreferredService(Activity activity, ComponentName service) {
    // Verify the activity is in the foreground before calling into NfcService
    if (activity == null || service == null) {
        throw new NullPointerException("activity or service or category is null");
    }
    if (!activity.isResumed()) {
        throw new IllegalArgumentException("Activity must be resumed.");
    }
    try {
        return sService.setPreferredService(service);
    } catch (RemoteException e) {
        // Try one more time
        recoverService();
        if (sService == null) {
            Log.e(TAG, "Failed to recover CardEmulationService.");
            return false;
        }
        try {
            return sService.setPreferredService(service);
        } catch (RemoteException ee) {
            Log.e(TAG, "Failed to reach CardEmulationService.");
            return false;
        }
    }
}
 
Example 7
Source File: NfcAdapter.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
void enforceResumed(Activity activity) {
    if (!activity.isResumed()) {
        throw new IllegalStateException("API cannot be called while activity is paused");
    }
}
 
Example 8
Source File: NfcAdapter.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Enable foreground dispatch to the given Activity.
 *
 * <p>This will give give priority to the foreground activity when
 * dispatching a discovered {@link Tag} to an application.
 *
 * <p>If any IntentFilters are provided to this method they are used to match dispatch Intents
 * for both the {@link NfcAdapter#ACTION_NDEF_DISCOVERED} and
 * {@link NfcAdapter#ACTION_TAG_DISCOVERED}. Since {@link NfcAdapter#ACTION_TECH_DISCOVERED}
 * relies on meta data outside of the IntentFilter matching for that dispatch Intent is handled
 * by passing in the tech lists separately. Each first level entry in the tech list represents
 * an array of technologies that must all be present to match. If any of the first level sets
 * match then the dispatch is routed through the given PendingIntent. In other words, the second
 * level is ANDed together and the first level entries are ORed together.
 *
 * <p>If you pass {@code null} for both the {@code filters} and {@code techLists} parameters
 * that acts a wild card and will cause the foreground activity to receive all tags via the
 * {@link NfcAdapter#ACTION_TAG_DISCOVERED} intent.
 *
 * <p>This method must be called from the main thread, and only when the activity is in the
 * foreground (resumed). Also, activities must call {@link #disableForegroundDispatch} before
 * the completion of their {@link Activity#onPause} callback to disable foreground dispatch
 * after it has been enabled.
 *
 * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
 *
 * @param activity the Activity to dispatch to
 * @param intent the PendingIntent to start for the dispatch
 * @param filters the IntentFilters to override dispatching for, or null to always dispatch
 * @param techLists the tech lists used to perform matching for dispatching of the
 *      {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
 * @throws IllegalStateException if the Activity is not currently in the foreground
 * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
 */
public void enableForegroundDispatch(Activity activity, PendingIntent intent,
        IntentFilter[] filters, String[][] techLists) {
    synchronized (NfcAdapter.class) {
        if (!sHasNfcFeature) {
            throw new UnsupportedOperationException();
        }
    }
    if (activity == null || intent == null) {
        throw new NullPointerException();
    }
    if (!activity.isResumed()) {
        throw new IllegalStateException("Foreground dispatch can only be enabled " +
                "when your activity is resumed");
    }
    try {
        TechListParcel parcel = null;
        if (techLists != null && techLists.length > 0) {
            parcel = new TechListParcel(techLists);
        }
        ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity,
                mForegroundDispatchListener);
        sService.setForegroundDispatch(intent, filters, parcel);
    } catch (RemoteException e) {
        attemptDeadServiceRecovery(e);
    }
}