Example usage for android.app TaskStackBuilder create

List of usage examples for android.app TaskStackBuilder create

Introduction

In this page you can find the example usage for android.app TaskStackBuilder create.

Prototype

public static TaskStackBuilder create(Context context) 

Source Link

Document

Return a new TaskStackBuilder for launching a fresh task stack consisting of a series of activities.

Usage

From source file:com.yeldi.yeldibazaar.UpdateService.java

protected void onHandleIntent(Intent intent) {

    receiver = intent.getParcelableExtra("receiver");

    long startTime = System.currentTimeMillis();
    String errmsg = "";
    try {/* www.  j av a 2 s  . com*/

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        // See if it's time to actually do anything yet...
        if (isScheduledRun()) {
            long lastUpdate = prefs.getLong("lastUpdateCheck", 0);
            String sint = prefs.getString("updateInterval", "0");
            int interval = Integer.parseInt(sint);
            if (interval == 0) {
                Log.d("FDroid", "Skipping update - disabled");
                return;
            }
            long elapsed = System.currentTimeMillis() - lastUpdate;
            if (elapsed < interval * 60 * 60 * 1000) {
                Log.d("FDroid",
                        "Skipping update - done " + elapsed + "ms ago, interval is " + interval + " hours");
                return;
            }
        } else {
            Log.d("FDroid", "Unscheduled (manually requested) update");
        }

        boolean notify = prefs.getBoolean("updateNotify", false);

        // Grab some preliminary information, then we can release the
        // database while we do all the downloading, etc...
        int prevUpdates = 0;
        int newUpdates = 0;
        List<DB.Repo> repos;
        try {
            DB db = DB.getDB();
            repos = db.getRepos();
        } finally {
            DB.releaseDB();
        }

        // Process each repo...
        List<DB.App> apps = new ArrayList<DB.App>();
        List<Integer> keeprepos = new ArrayList<Integer>();
        boolean success = true;
        boolean changes = false;
        for (DB.Repo repo : repos) {
            if (repo.inuse) {

                sendStatus(STATUS_INFO, getString(R.string.status_connecting_to_repo, repo.address));

                StringBuilder newetag = new StringBuilder();
                String err = RepoXMLHandler.doUpdate(getBaseContext(), repo, apps, newetag, keeprepos, this);
                if (err == null) {
                    String nt = newetag.toString();
                    if (!nt.equals(repo.lastetag)) {
                        repo.lastetag = newetag.toString();
                        changes = true;
                    }
                } else {
                    success = false;
                    err = "Update failed for " + repo.address + " - " + err;
                    Log.d("FDroid", err);
                    if (errmsg.length() == 0)
                        errmsg = err;
                    else
                        errmsg += "\n" + err;
                }
            }
        }

        List<DB.App> acceptedapps = new ArrayList<DB.App>();
        if (!changes && success) {
            Log.d("FDroid", "Not checking app details or compatibility, because all repos were up to date.");
        } else if (changes && success) {

            sendStatus(STATUS_INFO, getString(R.string.status_checking_compatibility));
            List<DB.App> prevapps = ((FDroidApp) getApplication()).getApps();

            DB db = DB.getDB();
            try {

                // Need to flag things we're keeping despite having received
                // no data about during the update. (i.e. stuff from a repo
                // that we know is unchanged due to the etag)
                for (int keep : keeprepos) {
                    for (DB.App app : prevapps) {
                        boolean keepapp = false;
                        for (DB.Apk apk : app.apks) {
                            if (apk.repo == keep) {
                                keepapp = true;
                                break;
                            }
                        }
                        if (keepapp) {
                            DB.App app_k = null;
                            for (DB.App app2 : apps) {
                                if (app2.id.equals(app.id)) {
                                    app_k = app2;
                                    break;
                                }
                            }
                            if (app_k == null) {
                                apps.add(app);
                                app_k = app;
                            }
                            app_k.updated = true;
                            db.populateDetails(app_k, keep);
                            for (DB.Apk apk : app.apks)
                                if (apk.repo == keep)
                                    apk.updated = true;
                        }
                    }
                }

                prevUpdates = db.beginUpdate(prevapps);
                for (DB.App app : apps) {
                    if (db.updateApplication(app))
                        acceptedapps.add(app);
                }
                db.endUpdate();
                if (notify)
                    newUpdates = db.getNumUpdates();
                for (DB.Repo repo : repos)
                    db.writeLastEtag(repo);
            } catch (Exception ex) {
                db.cancelUpdate();
                Log.e("FDroid", "Exception during update processing:\n" + Log.getStackTraceString(ex));
                errmsg = "Exception during processing - " + ex.getMessage();
                success = false;
            } finally {
                DB.releaseDB();
            }

        }

        if (success) {
            File d = DB.getIconsPath(this);
            List<DB.App> toDownloadIcons = null;
            if (!d.exists()) {
                Log.d("FDroid", "Icons were wiped. Re-downloading all of them.");
                d.mkdirs();
                toDownloadIcons = ((FDroidApp) getApplication()).getApps();
            } else if (changes) {
                toDownloadIcons = acceptedapps;
            }
            if (toDownloadIcons != null) {

                // Create a .nomedia file in the icons directory. For
                // recent Android versions this isn't necessary, because
                // they recognise the cache location. Older versions don't
                // though.
                File f = new File(d, ".nomedia");
                if (!f.exists()) {
                    try {
                        f.createNewFile();
                    } catch (Exception e) {
                        Log.d("FDroid", "Failed to create .nomedia");
                    }
                }

                sendStatus(STATUS_INFO, getString(R.string.status_downloading_icons));
                for (DB.App app : toDownloadIcons)
                    getIcon(app, repos);
            }
        }

        if (success && changes)
            ((FDroidApp) getApplication()).invalidateAllApps();

        if (success && changes && notify && (newUpdates > prevUpdates)) {
            Log.d("FDroid", "Notifying updates. Apps before:" + prevUpdates + ", apps after: " + newUpdates);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.icon)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
                    .setAutoCancel(true).setContentTitle(getString(R.string.fdroid_updates_available));
            Intent notifyIntent = new Intent(this, FDroid.class).putExtra(FDroid.EXTRA_TAB_UPDATE, true);
            if (newUpdates > 1) {
                mBuilder.setContentText(getString(R.string.many_updates_available, newUpdates));

            } else {
                mBuilder.setContentText(getString(R.string.one_update_available));
            }
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this).addParentStack(FDroid.class)
                    .addNextIntent(notifyIntent);
            PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(pendingIntent);
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                    Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());
        }

        if (!success) {
            if (errmsg.length() == 0)
                errmsg = "Unknown error";
            sendStatus(STATUS_ERROR, errmsg);
        } else {
            sendStatus(STATUS_COMPLETE);
            Editor e = prefs.edit();
            e.putLong("lastUpdateCheck", System.currentTimeMillis());
            e.commit();
        }

    } catch (Exception e) {
        Log.e("FDroid", "Exception during update processing:\n" + Log.getStackTraceString(e));
        if (errmsg.length() == 0)
            errmsg = "Unknown error";
        sendStatus(STATUS_ERROR, errmsg);
    } finally {
        Log.d("FDroid", "Update took " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds.");
        receiver = null;
    }
}

From source file:com.kdb.ledcontrol.LEDManager.java

public void setOnBoot() {
    String cmd = prefs.getString("last_cmd", null);
    int brightness = prefs.getInt("last_brightness", -1);
    if (brightness != -1 && devicePathToBrightness != null) {
        brightness /= 25;//from ww  w  .j  a  v  a2s . c  om
        ApplyBrightness(brightness);
    }
    if (cmd != null && devicePathToLED != null) {
        try {
            deviceInput.writeBytes("echo " + cmd + " > " + devicePathToLED + "\n");
            deviceInput.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (checkState() != -1 && prefs.getBoolean("show_notif", true)) {
        Log.i(TAG, "LED trigger applied on boot");
        Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_stat_notification).setLargeIcon(largeIcon)
                .setContentTitle(context.getString(R.string.app_name_full))
                .setContentText(context.getString(R.string.notification_content));
        Intent resultIntent = new Intent(context, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, builder.build());
    }
}

From source file:com.ithinkbest.taipeiok.NavigationDrawerActivity.java

private void notifyGooglePlay() {
    int idGooglePlay = 12345;
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.to_google_play));
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ToGooglePlayActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ToGooglePlayActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(idGooglePlay, mBuilder.build());

}

From source file:com.hmatalonga.greenhub.util.Notifier.java

public static void batteryHighTemperature(final Context context) {
    if (sNotificationManager == null) {
        sNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }//  w w w.  j a v  a  2  s  .c o  m

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_alert_circle_white_24dp)
            .setContentTitle(context.getString(R.string.notif_battery_hot))
            .setContentText(context.getString(R.string.notif_battery_cooldown)).setAutoCancel(true)
            .setOngoing(false).setLights(Color.RED, 500, 2000).setVibrate(new long[] { 0, 800, 1500 })
            .setPriority(SettingsUtils.fetchNotificationsPriority(context));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.putExtra("tab", 2);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(InboxActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);

    // Because the ID remains unchanged, the existing notification is updated.
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    sNotificationManager.notify(Config.NOTIFICATION_TEMPERATURE_HIGH, notification);
}

From source file:com.y59song.PrivacyGuard.MyVpnService.java

@Override
public void updateNotification(String appName, String msg) {
    DatabaseHandler db = new DatabaseHandler(this);

    boolean ignored = false;

    if (isIgnored(appName, msg)) {
        ignored = true;//from  w w w.jav  a  2s  .  com
    }

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    int notifyId = findNotificationId(appName, msg);
    int generalNotifyId = findGeneralNotificationId(appName);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this)
            .setContentText("Number of leaks: " + findNotificationCounter(appName, msg))
            .setContentTitle(appName + " " + msg).setTicker(appName + " " + msg)
            .setSmallIcon(R.drawable.notify);

    // ------------------------------------------------------------------------------------
    // Currently initiates a new activity instance each time, should recycle if already open
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, DetailsActivity.class); // should actually be DetailsActivity.class

    List<LocationLeak> leakList = db.getLocationLeaks(appName);
    resultIntent.putExtra(EXTRA_APP, appName);
    resultIntent.putExtra(EXTRA_SIZE, String.valueOf(leakList.size()));
    for (int i = 0; i < leakList.size(); i++) {
        resultIntent.putExtra(EXTRA_DATA + i, leakList.get(i).getLocation()); // to pass values between activities
    }

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(DetailsActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mNotifyBuilder.setContentIntent(resultPendingIntent);

    // ------------------------------------------------------------------------------------
    // Creates an explicit intent for an Activity in your app
    Intent intent = new Intent(this, ActionReceiver.class);
    intent.setAction("Ignore");
    intent.putExtra("appName", appName);
    intent.putExtra("notificationId", notifyId);

    // use System.currentTimeMillis() to have a unique ID for the pending intent
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(),
            (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mNotifyBuilder.addAction(R.drawable.ignore, "Ignore", pendingIntent);

    // ------------------------------------------------------------------------------------
    // Set Notification Style to Expanded
    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

    String[] events = new String[findNotificationCounter(appName, msg)];
    if (generalNotifyId >= 0 && !ignored) {
        // Because the ID remains unchanged, the existing notification is updated.
        Log.i(TAG, "NOTIFYID IS SUCCESSFULL" + notifyId);
        mNotificationManager.notify(generalNotifyId, mNotifyBuilder.build());
    } else {
        Log.i(TAG, "NOTIFYID IS FAILING" + notifyId);
    }

    // -----------------------------------------------------------------------
    // Database Entry

    db.addLeak(new DataLeak(notifyId, appName, msg, events.length, dateFormat.format(new Date())));

    // -----------------------------------------------------------------------
}

From source file:com.ithinkbest.taipeiok.NavigationDrawerActivity.java

private void notifyAppWebpage() {
    int idGooglePlay = 12346;
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.to_app_webpage));
    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, ToAppWebpageActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ToAppWebpageActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(idGooglePlay, mBuilder.build());

}

From source file:com.google.android.apps.forscience.whistlepunk.project.experiment.ExperimentDetailsFragment.java

private void goToProjectDetails() {
    Intent upIntent = NavUtils.getParentActivityIntent(getActivity());
    upIntent.putExtra(ProjectDetailsFragment.ARG_PROJECT_ID, mExperiment.getProjectId());
    if (NavUtils.shouldUpRecreateTask(getActivity(), upIntent)
            || getArguments().getBoolean(ARG_CREATE_TASK, false)) {
        TaskStackBuilder.create(getActivity()).addNextIntentWithParentStack(upIntent).startActivities();
    } else {/*from  w  ww . ja  va2  s .  c  o  m*/
        NavUtils.navigateUpTo(getActivity(), upIntent);
    }
}

From source file:com.polkapolka.bluetooth.le.DeviceControlActivity.java

public static void showNotificationInMenu(Context context) {

    // variables//from  ww  w .  j  ava2  s .  c o m
    long currentTime = System.currentTimeMillis();

    // guard: check if we should wait
    if (currentTime - lastNotificationTime < NOTIFICATION_DELAY) {
        return;
    }

    lastNotificationTime = currentTime;

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
            .setContentTitle(context.getString(R.string.NotifationTitle))
            .setContentText(context.getString(R.string.NotificationSubtitle))
            .setTicker(context.getString(R.string.BadPostureTicker)).setSmallIcon(R.drawable.icon);
    // Define that we have the intention of opening MoreInfoNotification
    Intent moreInfoIntent = new Intent(context, userActivity.class);

    // Used to stack tasks across activites so we go to the proper place when back is clicked
    TaskStackBuilder tStackBuilder = TaskStackBuilder.create(context);

    // Add all parents of this activity to the stack
    tStackBuilder.addParentStack(DeviceControlActivity.class);

    // Add our new Intent to the stack
    tStackBuilder.addNextIntent(moreInfoIntent);
    // default_all -> vibrate light and sound DEFAULT_VIBRATE -> only vibration even with sound on.
    notificationBuilder.setDefaults(Notification.DEFAULT_ALL);

    notificationBuilder.setAutoCancel(true);

    // Define an Intent and an action to perform with it by another application
    // FLAG_UPDATE_CURRENT : If the intent exists keep it but update it if needed
    PendingIntent pendingIntent = tStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Defines the Intent to fire when the notification is clicked

    notificationBuilder.setContentIntent(pendingIntent);

    // Gets a NotificationManager which is used to notify the user of the background event

    NotificationManager notificationManager;
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Post the notification
    notificationManager.notify(notifID, notificationBuilder.build());

}

From source file:com.radiusnetworks.museumguide.MuseumGuideApplication.java

private void sendNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle("Museum Guide")
            .setContentText("An exhibit item is nearby.").setSmallIcon(R.drawable.launcher);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(new Intent(this, MuseumItemsActivity.class));
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);
    NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());
}

From source file:com.polkapolka.bluetooth.le.DeviceControlActivity.java

public void showNotification(View view) {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.NotifationTitle))
            .setContentText(getString(R.string.NotificationSubtitle))
            .setTicker(getString(R.string.NotificationTicker)).setSmallIcon(R.drawable.icon);
    // Define that we have the intention of opening MoreInfoNotification
    Intent moreInfoIntent = new Intent(this, userActivity.class);

    // Used to stack tasks across activites so we go to the proper place when back is clicked
    TaskStackBuilder tStackBuilder = TaskStackBuilder.create(this);

    // Add all parents of this activity to the stack
    tStackBuilder.addParentStack(DeviceControlActivity.class);

    // Add our new Intent to the stack
    tStackBuilder.addNextIntent(moreInfoIntent);
    notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
    notificationBuilder.setAutoCancel(true);

    // Define an Intent and an action to perform with it by another application
    // FLAG_UPDATE_CURRENT : If the intent exists keep it but update it if needed
    PendingIntent pendingIntent = tStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Defines the Intent to fire when the notification is clicked

    notificationBuilder.setContentIntent(pendingIntent);

    // Gets a NotificationManager which is used to notify the user of the background event
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Post the notification
    notificationManager.notify(notifID, notificationBuilder.build());

}