Example usage for android.app PendingIntent getTargetPackage

List of usage examples for android.app PendingIntent getTargetPackage

Introduction

In this page you can find the example usage for android.app PendingIntent getTargetPackage.

Prototype

@Deprecated
public String getTargetPackage() 

Source Link

Usage

From source file:Main.java

/**
 * @see android.app.PendingIntent#getCreatorPackage()
 *///from   w w  w  .j a va2s.  c o  m
@SuppressWarnings("deprecation")
public static String getCreatorPackage(PendingIntent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return intent.getCreatorPackage();
    } else {
        return intent.getTargetPackage();
    }
}

From source file:android.support.customtabs.browseractions.BrowserActionsIntent.java

/**
 * Get the package name of the creator application.
 * @param intent The {@link BrowserActionsIntent}.
 * @return The creator package name./* ww  w  . j a va  2 s  .c om*/
 */
@SuppressWarnings("deprecation")
public static String getCreatorPackageName(Intent intent) {
    PendingIntent pendingIntent = intent.getParcelableExtra(BrowserActionsIntent.EXTRA_APP_ID);
    if (pendingIntent != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return pendingIntent.getCreatorPackage();
        } else {
            return pendingIntent.getTargetPackage();
        }
    }
    return null;
}

From source file:de.tubs.ibr.dtn.service.DaemonService.java

/**
 * Incoming Intents are handled here/*from w  w w .ja  v  a  2 s .  co  m*/
 * 
 * @param intent
 */
@SuppressWarnings("deprecation")
public void onHandleIntent(Intent intent, int startId) {
    String action = intent.getAction();

    if (ACTION_STARTUP.equals(action)) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        // only start if the daemon is offline or in error state
        // and if the daemon switch is on
        if ((mDaemonProcess.getState().equals(DaemonState.OFFLINE)
                || mDaemonProcess.getState().equals(DaemonState.ERROR))
                && prefs.getBoolean(Preferences.KEY_ENABLED, true)) {
            // start-up the daemon
            mDaemonProcess.start();

            final Intent storeStatsIntent = new Intent(this, DaemonService.class);
            storeStatsIntent.setAction(de.tubs.ibr.dtn.service.DaemonService.ACTION_STORE_STATS);
            startService(storeStatsIntent);
        }
    } else if (ACTION_SHUTDOWN.equals(action)) {
        // stop main loop
        mDaemonProcess.stop();
    } else if (ACTION_RESTART.equals(action)) {
        Integer level = intent.getIntExtra("runlevel", 0);

        // restart the daemon into the given runlevel
        mDaemonProcess.restart(level, new DaemonProcess.OnRestartListener() {
            @Override
            public void OnStop(DaemonRunLevel previous, DaemonRunLevel next) {
                if (next.swigValue() < DaemonRunLevel.RUNLEVEL_API.swigValue()
                        && previous.swigValue() >= DaemonRunLevel.RUNLEVEL_API.swigValue()) {
                    // shutdown the session manager
                    mSessionManager.destroy();
                }
            }

            @Override
            public void OnStart(DaemonRunLevel previous, DaemonRunLevel next) {
                if (previous.swigValue() < DaemonRunLevel.RUNLEVEL_API.swigValue()
                        && next.swigValue() >= DaemonRunLevel.RUNLEVEL_API.swigValue()) {
                    // re-initialize the session manager
                    mSessionManager.initialize();
                }
            }

            @Override
            public void OnReloadConfiguration() {
            }
        });

        final Intent storeStatsIntent = new Intent(this, DaemonService.class);
        storeStatsIntent.setAction(de.tubs.ibr.dtn.service.DaemonService.ACTION_STORE_STATS);
        startService(storeStatsIntent);
    } else if (ACTION_UPDATE_NOTIFICATION.equals(action)) {
        if (intent.hasExtra("text")) {
            // update state text in the notification
            updateNotification(intent.getStringExtra("text"));
        } else {
            // update state text in the notification
            updateNotification(null);
        }
    } else if (de.tubs.ibr.dtn.Intent.REGISTER.equals(action)) {
        final Registration reg = (Registration) intent.getParcelableExtra("registration");
        final PendingIntent pi = (PendingIntent) intent.getParcelableExtra("app");

        mSessionManager.register(pi.getTargetPackage(), reg);

    } else if (de.tubs.ibr.dtn.Intent.UNREGISTER.equals(action)) {
        final PendingIntent pi = (PendingIntent) intent.getParcelableExtra("app");

        mSessionManager.unregister(pi.getTargetPackage());
    } else if (ACTION_INITIATE_CONNECTION.equals(action)) {
        if (intent.hasExtra("endpoint")) {
            mDaemonProcess.initiateConnection(intent.getStringExtra("endpoint"));
        }
    } else if (ACTION_CLEAR_STORAGE.equals(action)) {
        mDaemonProcess.clearStorage();
        Toast.makeText(this, R.string.toast_storage_cleared, Toast.LENGTH_SHORT).show();
    } else if (ACTION_NETWORK_CHANGED.equals(action)) {
        // This intent tickle the service if something has changed in the
        // network configuration. If the service was enabled but terminated
        // before,
        // it will be started now.

        final Intent storeStatsIntent = new Intent(this, DaemonService.class);
        storeStatsIntent.setAction(de.tubs.ibr.dtn.service.DaemonService.ACTION_STORE_STATS);
        startService(storeStatsIntent);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        // if discovery is configured as "smart"
        if ("smart".equals(prefs.getString(Preferences.KEY_DISCOVERY_MODE, "smart"))) {
            // enable discovery for 2 minutes
            final Intent discoIntent = new Intent(DaemonService.this, DaemonService.class);
            discoIntent.setAction(de.tubs.ibr.dtn.service.DaemonService.ACTION_START_DISCOVERY);
            discoIntent.putExtra(EXTRA_DISCOVERY_DURATION, 120L);
            startService(discoIntent);
        }
    } else if (ACTION_STORE_STATS.equals(action)) {
        // cancel the next scheduled collection
        mServiceHandler.removeCallbacks(mCollectStats);

        if (mStatsLastAction != null) {
            Calendar now = Calendar.getInstance();
            now.roll(Calendar.MINUTE, -1);
            if (mStatsLastAction.before(now.getTime())) {
                refreshStats();
            }
        } else {
            refreshStats();
        }

        // schedule next collection in 15 minutes
        mServiceHandler.postDelayed(mCollectStats, 900000);
    } else if (ACTION_INITIALIZE.equals(action)) {
        // initialize the daemon service
        initialize();
    } else if (ACTION_START_DISCOVERY.equals(action)) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String discoMode = prefs.getString(Preferences.KEY_DISCOVERY_MODE, "smart");
        boolean stayOn = "on".equals(discoMode) && mDiscoveryState;

        // create stop discovery intent
        Intent stopIntent = new Intent(this, DaemonService.class);
        stopIntent.setAction(DaemonService.ACTION_STOP_DISCOVERY);

        if (intent.hasExtra(EXTRA_DISCOVERY_DURATION) && !stayOn) {
            Long duration = intent.getLongExtra(EXTRA_DISCOVERY_DURATION, 120);

            Message msg = mServiceHandler.obtainMessage();
            msg.what = MSG_WHAT_STOP_DISCOVERY;
            msg.arg1 = startId;
            msg.obj = stopIntent;
            mServiceHandler.sendMessageDelayed(msg, duration * 1000);

            Log.i(TAG, "Discovery stop scheduled in " + duration + " seconds.");
        } else {
            mServiceHandler.removeMessages(MSG_WHAT_STOP_DISCOVERY);
            Log.i(TAG, "Scheduled discovery stop removed.");
        }

        // only start discovery if not active
        if (!mDiscoveryState) {
            // start P2P discovery and enable IPND
            mDaemonProcess.startDiscovery();

            // start Wi-Fi P2P discovery
            if (mP2pManager != null)
                mP2pManager.startDiscovery();

            // set global discovery state
            mDiscoveryState = true;

            // request notification update
            requestNotificationUpdate(getResources().getString(R.string.ticker_discovery_started));
        }
    } else if (ACTION_STOP_DISCOVERY.equals(action)) {
        // only stop discovery if active
        if (mDiscoveryState) {
            // stop P2P discovery and disable IPND
            mDaemonProcess.stopDiscovery();

            // stop Wi-Fi P2P discovery
            if (mP2pManager != null)
                mP2pManager.stopDiscovery();

            // set global discovery state
            mDiscoveryState = false;

            // request notification update
            requestNotificationUpdate(getResources().getString(R.string.ticker_discovery_stopped));
        }

        // remove all stop discovery messages
        mServiceHandler.removeMessages(MSG_WHAT_STOP_DISCOVERY);
        Log.i(TAG, "Scheduled discovery stop removed.");
    }

    // stop the daemon if it should be offline
    if (mDaemonProcess.getState().equals(DaemonState.OFFLINE) && (startId != -1))
        stopSelf(startId);
}

From source file:edu.mit.media.funf.probe.Probe.java

/**
 * Updates request list with items in queue, replacing duplicate pending intents for this probe.
 * @param requests/*from  www . ja  va2 s .co  m*/
 */
private void updateRequests(boolean removeRunOnce) {
    assert requestsIntent != null;
    boolean hasChanges = false;
    ArrayList<Intent> requests = requestsIntent.getParcelableArrayListExtra(INTERNAL_REQUESTS_KEY);
    if (requests == null) {
        hasChanges = true;
        requests = new ArrayList<Intent>();
    }

    // Remove run once requests
    Parameter periodParam = Parameter.getAvailableParameter(getAvailableParameters(), Parameter.Builtin.PERIOD);
    if (periodParam != null && removeRunOnce) {
        for (Intent request : requests) {
            ArrayList<Bundle> dataRequests = Utils.getArrayList(request.getExtras(), REQUESTS_KEY);
            List<Bundle> runOnceDataRequests = new ArrayList<Bundle>();
            for (Bundle dataRequest : dataRequests) {
                long periodValue = Utils.getLong(dataRequest, Parameter.Builtin.PERIOD.name,
                        (Long) periodParam.getValue());
                if (periodValue == 0L) {
                    Log.d(TAG, "Removing run once dataRequest: " + dataRequest);
                    runOnceDataRequests.add(dataRequest);
                }
            }
            dataRequests.removeAll(runOnceDataRequests);
            if (dataRequests.isEmpty()) {
                deadRequests.add(request);
            } else {
                request.putExtra(REQUESTS_KEY, dataRequests);
            }
        }
    }

    // Remove all requests that we aren't able to (or supposed to) send to anymore
    if (!deadRequests.isEmpty()) {
        hasChanges = true;
        for (Intent deadRequest = deadRequests.poll(); deadRequest != null; deadRequest = deadRequests.poll()) {
            Log.d(TAG, "Removing dead request: " + deadRequest);
            requests.remove(deadRequest);
        }
    }
    // Add any pending requests
    if (!pendingRequests.isEmpty()) {
        hasChanges = true;
        Map<PendingIntent, Intent> existingCallbacksToRequests = new HashMap<PendingIntent, Intent>();
        for (Intent existingRequest : requests) {
            PendingIntent callback = existingRequest.getParcelableExtra(CALLBACK_KEY);
            existingCallbacksToRequests.put(callback, existingRequest);
        }
        for (Intent request = pendingRequests.poll(); request != null; request = pendingRequests.poll()) {
            PendingIntent callback = request.getParcelableExtra(CALLBACK_KEY);
            if (packageHasRequiredPermissions(this, callback.getTargetPackage(), getRequiredPermissions())) {
                existingCallbacksToRequests.containsKey(callback);
                int existingRequestIndex = requests.indexOf(existingCallbacksToRequests.get(callback));
                ArrayList<Bundle> dataRequests = Utils.getArrayList(request.getExtras(), REQUESTS_KEY);
                Log.d(TAG, "Adding pending intent with data requests: " + dataRequests);
                if (existingRequestIndex >= 0) {
                    if (dataRequests == null || dataRequests.isEmpty()) {
                        Log.d(TAG, "Adding pending intent, removing because empty or null");
                        requests.remove(existingRequestIndex);
                    } else {
                        requests.set(existingRequestIndex, request);
                    }
                } else {
                    if (dataRequests != null && !dataRequests.isEmpty()) { // Only add requests with nonempty data requests
                        Log.d(TAG, "Adding new pending intent: " + request);
                        requests.add(request);
                    }
                }
            } else {
                Log.w(TAG, "Package '" + callback.getTargetPackage()
                        + "' does not have the required permissions to get data from this probe.");
            }
        }
    }

    if (hasChanges) {
        requestsIntent.putExtra(INTERNAL_REQUESTS_KEY, requests);
        updateInternalRequestsPendingIntent();
    }
}

From source file:edu.mit.media.funf.probe.Probe.java

/**
 * Returns true if request was successfully queued
 * @param request/*from   www. java2 s .c o  m*/
 * @return
 */
private boolean queueRequest(Intent request) {
    // Check for pending intent
    PendingIntent callback = null;
    try {
        callback = request.getParcelableExtra(CALLBACK_KEY);
    } catch (Exception e) {
        Log.e(TAG, "Request sent invalid callback.");
    }

    if (callback == null) {
        Log.e(TAG, "Request did not send callback.");
    } else {
        boolean succesfullyQueued = pendingRequests.offer(request);
        if (succesfullyQueued) {
            Log.i(TAG, "Queued request from package '" + callback.getTargetPackage() + "'");
            return true;
        } else {
            Log.e(TAG, "Unable to queue request from package '" + callback.getTargetPackage() + "'");
        }
    }
    return false;
}