List of usage examples for android.content SharedPreferences getInt
int getInt(String key, int defValue);
From source file:com.klinker.android.twitter.utils.NotificationUtils.java
public static void refreshNotification(Context context, boolean noTimeline) { AppSettings settings = AppSettings.getInstance(context); SharedPreferences sharedPrefs = context.getSharedPreferences( "com.klinker.android.twitter_world_preferences", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE); int currentAccount = sharedPrefs.getInt("current_account", 1); //int[] unreadCounts = new int[] {4, 1, 2}; // for testing int[] unreadCounts = getUnreads(context); int timeline = unreadCounts[0]; int realTimelineCount = timeline; // if they don't want that type of notification, simply set it to zero if (!settings.timelineNot || (settings.pushNotifications && settings.liveStreaming) || noTimeline) { unreadCounts[0] = 0;//from www .j a v a2 s.c o m } if (!settings.mentionsNot) { unreadCounts[1] = 0; } if (!settings.dmsNot) { unreadCounts[2] = 0; } if (unreadCounts[0] == 0 && unreadCounts[1] == 0 && unreadCounts[2] == 0) { } else { Intent markRead = new Intent(context, MarkReadService.class); PendingIntent readPending = PendingIntent.getService(context, 0, markRead, 0); String shortText = getShortText(unreadCounts, context, currentAccount); String longText = getLongText(unreadCounts, context, currentAccount); // [0] is the full title and [1] is the screenname String[] title = getTitle(unreadCounts, context, currentAccount); boolean useExpanded = useExp(context); boolean addButton = addBtn(unreadCounts); if (title == null) { return; } Intent resultIntent; if (unreadCounts[1] != 0 && unreadCounts[0] == 0) { // it is a mention notification (could also have a direct message) resultIntent = new Intent(context, RedirectToMentions.class); } else if (unreadCounts[2] != 0 && unreadCounts[0] == 0 && unreadCounts[1] == 0) { // it is a direct message resultIntent = new Intent(context, RedirectToDMs.class); } else { resultIntent = new Intent(context, MainActivity.class); } PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0); NotificationCompat.Builder mBuilder; Intent deleteIntent = new Intent(context, NotificationDeleteReceiverOne.class); mBuilder = new NotificationCompat.Builder(context).setContentTitle(title[0]) .setContentText(TweetLinkUtils.removeColorHtml(shortText, settings)) .setSmallIcon(R.drawable.ic_stat_icon).setLargeIcon(getIcon(context, unreadCounts, title[1])) .setContentIntent(resultPendingIntent).setAutoCancel(true) .setTicker(TweetLinkUtils.removeColorHtml(shortText, settings)) .setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)) .setPriority(NotificationCompat.PRIORITY_HIGH); if (unreadCounts[1] > 1 && unreadCounts[0] == 0 && unreadCounts[2] == 0) { // inbox style notification for mentions mBuilder.setStyle(getMentionsInboxStyle(unreadCounts[1], currentAccount, context, TweetLinkUtils.removeColorHtml(shortText, settings))); } else if (unreadCounts[2] > 1 && unreadCounts[0] == 0 && unreadCounts[1] == 0) { // inbox style notification for direct messages mBuilder.setStyle(getDMInboxStyle(unreadCounts[1], currentAccount, context, TweetLinkUtils.removeColorHtml(shortText, settings))); } else { // big text style for an unread count on timeline, mentions, and direct messages mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml( settings.addonTheme ? longText.replaceAll("FF8800", settings.accentColor) : longText))); } // Pebble notification if (sharedPrefs.getBoolean("pebble_notification", false)) { sendAlertToPebble(context, title[0], shortText); } // Light Flow notification sendToLightFlow(context, title[0], shortText); int homeTweets = unreadCounts[0]; int mentionsTweets = unreadCounts[1]; int dmTweets = unreadCounts[2]; int newC = 0; if (homeTweets > 0) { newC++; } if (mentionsTweets > 0) { newC++; } if (dmTweets > 0) { newC++; } if (settings.notifications && newC > 0) { if (settings.vibrate) { mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); } if (settings.sound) { try { mBuilder.setSound(Uri.parse(settings.ringtone)); } catch (Exception e) { mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); } } if (settings.led) mBuilder.setLights(0xFFFFFF, 1000, 1000); // Get an instance of the NotificationManager service NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); if (addButton) { // the reply and read button should be shown Intent reply; if (unreadCounts[1] == 1) { reply = new Intent(context, NotificationCompose.class); } else { reply = new Intent(context, NotificationDMCompose.class); } Log.v("username_for_noti", title[1]); sharedPrefs.edit().putString("from_notification", "@" + title[1] + " " + title[2]).commit(); MentionsDataSource data = MentionsDataSource.getInstance(context); long id = data.getLastIds(currentAccount)[0]; PendingIntent replyPending = PendingIntent.getActivity(context, 0, reply, 0); sharedPrefs.edit().putLong("from_notification_long", id).commit(); sharedPrefs.edit() .putString("from_notification_text", "@" + title[1] + ": " + TweetLinkUtils.removeColorHtml(shortText, settings)) .commit(); // Create the remote input RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY) .setLabel("@" + title[1] + " ").build(); // Create the notification action NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder( R.drawable.ic_action_reply_dark, context.getResources().getString(R.string.noti_reply), replyPending).addRemoteInput(remoteInput).build(); NotificationCompat.Action.Builder action = new NotificationCompat.Action.Builder( R.drawable.ic_action_read_dark, context.getResources().getString(R.string.mark_read), readPending); mBuilder.addAction(replyAction); mBuilder.addAction(action.build()); } else { // otherwise, if they can use the expanded notifications, the popup button will be shown Intent popup = new Intent(context, RedirectToPopup.class); popup.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); popup.putExtra("from_notification", true); PendingIntent popupPending = PendingIntent.getActivity(context, 0, popup, 0); NotificationCompat.Action.Builder action = new NotificationCompat.Action.Builder( R.drawable.ic_popup, context.getResources().getString(R.string.popup), popupPending); mBuilder.addAction(action.build()); } // Build the notification and issues it with notification manager. notificationManager.notify(1, mBuilder.build()); // if we want to wake the screen on a new message if (settings.wakeScreen) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); final PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG"); wakeLock.acquire(5000); } } // if there are unread tweets on the timeline, check them for favorite users if (settings.favoriteUserNotifications && realTimelineCount > 0) { favUsersNotification(currentAccount, context); } } try { ContentValues cv = new ContentValues(); cv.put("tag", "com.klinker.android.twitter/com.klinker.android.twitter.ui.MainActivity"); // add the direct messages and mentions cv.put("count", unreadCounts[1] + unreadCounts[2]); context.getContentResolver().insert(Uri.parse("content://com.teslacoilsw.notifier/unread_count"), cv); } catch (IllegalArgumentException ex) { /* Fine, TeslaUnread is not installed. */ } catch (Exception ex) { /* Some other error, possibly because the format of the ContentValues are incorrect. Log but do not crash over this. */ ex.printStackTrace(); } }
From source file:ch.christofbuechi.testgcm.MainActivity.java
/** * Gets the current registration ID for application on GCM service, if there is one. * <p>/*w w w .j a v a2s . co m*/ * If result is empty, the app needs to register. * * @return registration ID, or empty string if there is no existing * registration ID. */ private String getRegistrationId(Context context) { final SharedPreferences prefs = getGcmPreferences(context); String registrationId = prefs.getString(PROPERTY_REG_ID, ""); if (registrationId.isEmpty()) { Log.i(TAG, "Registration not found."); return ""; } // Check if app was updated; if so, it must clear the registration ID // since the existing regID is not guaranteed to work with the new // app version. int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); int currentVersion = getAppVersion(context); if (registeredVersion != currentVersion) { Log.i(TAG, "App version changed."); return ""; } return registrationId; }
From source file:com.intel.xdk.camera.Camera.java
public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == SELECT_PICTURE) { imageSelected(data);/* ww w . j a v a 2s. com*/ } else { //get info from shared prefs SharedPreferences prefs = activity.getSharedPreferences(cameraPrefsKey, 0); String outputFile = prefs.getString(cameraPrefsFileName, ""); boolean isPNG = prefs.getBoolean(cameraPrefsIsPNG, false); int quality = prefs.getInt(cameraPrefsQuality, 100); savePicture(outputFile, quality, isPNG); } } else { pictureCancelled(); } }
From source file:au.id.tedp.mapdroid.Picker.java
private MapLocation getInitialLocation(Bundle savedState) { double newLat, newLong; int newZoom;// w w w. ja v a2 s .c om // Default location: Germany, from a high zoom level. // OpenStreetMap is big in Germany. newLat = 52.5; newLong = 13.4; newZoom = 2; SharedPreferences settings = getPreferences(MODE_PRIVATE); if (savedState != null) { newLat = savedState.getFloat(SAVED_LATITUDE_KEY, (float) newLat); newLong = savedState.getFloat(SAVED_LONGITUDE_KEY, (float) newLong); newZoom = savedState.getInt(SAVED_ZOOM_KEY, newZoom); } else if (settings != null) { newLat = settings.getFloat(SAVED_LATITUDE_KEY, (float) newLat); newLong = settings.getFloat(SAVED_LONGITUDE_KEY, (float) newLong); newZoom = settings.getInt(SAVED_ZOOM_KEY, newZoom); } MapLocation l = new MapLocation(new Location("default"), newZoom); l.setLatitude(newLat); l.setLongitude(newLong); return l; }
From source file:com.juanojfp.gcmsample.MainActivity.java
/** * Gets the current registration id for application on GCM service. * <p>/*from w w w. ja v a 2s . c om*/ * If result is empty, the registration has failed. * * @return registration id, or empty string if the registration is not * complete. */ private String getRegistrationId(Context context) { final SharedPreferences prefs = getGCMPreferences(context); String registrationId = prefs.getString(PROPERTY_REG_ID, ""); if (registrationId.length() == 0) { Log.v(TAG, "Registration not found."); return ""; } // check if app was updated; if so, it must clear registration id to // avoid a race condition if GCM sends a message int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); int currentVersion = getAppVersion(context); if (registeredVersion != currentVersion || isRegistrationExpired()) { Log.v(TAG, "App version changed or registration expired."); return ""; } return registrationId; }
From source file:com.ac.vct.pharmacylog.services.IMService.java
public String requestReloadAllDataHTTP(String username, String touserName, String message) throws UnsupportedEncodingException { SharedPreferences settings = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE); int iCurReloadDays = settings.getInt(FrmSettings.STR_RELOADALL_DAYS, 5); String reloadText = "Days:" + String.valueOf(iCurReloadDays); if (message.length() == 0) message = reloadText; //e.g. "Days:1 String params = "username=" + URLEncoder.encode(this.username, "UTF-8") + "&password=" + URLEncoder.encode(this.password, "UTF-8") + "&to=" + URLEncoder.encode(touserName, "UTF-8") + "&message=" + URLEncoder.encode(message, "UTF-8") + "&action=" + URLEncoder.encode("reloadAllMessages", "UTF-8") + "&usertype=" + URLEncoder.encode(USER_TYPE, "UTF-8") + "&"; //Log.i("Reload PARAMS", params); rawMessageList = socketOperator.sendHttpRequest(params); //rawMessageList = rawMessageList.replace("&", "&"); //Log.i("Reload Data", rawMessageList); if (rawMessageList != null) { this.parseMessageInfo(rawMessageList); }/*from w w w . jav a 2s. co m*/ return rawMessageList; }
From source file:fr.mdk.kisspush.KISSPush.java
/** * Gets the current registration ID for application on GCM service, if there * is one.// www. j av a2 s . c om * <p> * If result is empty, the app needs to register. * * @return registration ID, or empty string if there is no existing * registration ID. */ private String getRegistrationId(Context context) { final SharedPreferences prefs = getGcmPreferences(context); String registrationId = prefs.getString(PROPERTY_REG_ID, ""); if (registrationId.isEmpty()) { Log.i(TAG, "Registration not found."); return ""; } // Check if app was updated; if so, it must clear the registration ID // since the existing regID is not guaranteed to work with the new // app version. int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); int currentVersion = getAppVersion(context); if (registeredVersion != currentVersion) { Log.i(TAG, "App version changed."); return ""; } Log.i(TAG, "Registration ID found in SharedPreferences: " + registrationId); return registrationId; }
From source file:com.mobilyzer.measurements.RRCTask.java
/** * Keep a global counter that labels each test with a unique, increasing integer. * // w ww .j a v a2 s . c om * @param context Any context instance, needed to fetch the last test id from permanent storage * @return The unique (for this device) test ID generated. */ public static synchronized int getTestId(Context context) { SharedPreferences prefs = context.getSharedPreferences("test_ids", Context.MODE_PRIVATE); int testid = prefs.getInt("test_id", 0) + 1; SharedPreferences.Editor editor = prefs.edit(); editor.putInt("test_id", testid); editor.commit(); return testid; }
From source file:com.ac.vct.pharmacylog.services.IMService.java
/** * Show a notification while this service is running. * @param msg /*from w w w .j av a2s . c om*/ **/ private void showNotification(String username, String msg) { SharedPreferences settings = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE); int bShowNotification = settings.getInt(FrmSettings.NOTIFICATION_PREFERENCE, 0); if (0 == bShowNotification) return; // Set the icon, scrolling text and TIMESTAMP String title = "PharmacyLog: You got a new Message! (" + username + ")"; String text = username + ": " + ((msg.length() < 5) ? msg : msg.substring(0, 5) + "..."); //NotificationCompat.Builder notification = new NotificationCompat.Builder(R.drawable.stat_sample, title,System.currentTimeMillis()); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.stat_sample).setContentTitle(title).setContentText(text); Intent i = new Intent(this, Messaging.class); i.putExtra(FriendInfo.USERNAME, username); i.putExtra(MessageInfo.MESSAGETEXT, msg); // The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0); // Set the info for the views that show in the notification panel. // msg.length()>15 ? MSG : msg.substring(0, 15); mBuilder.setContentIntent(contentIntent); mBuilder.setContentText("New message from " + username + ": " + msg); //TODO: it can be improved, for instance message coming from same user may be concatenated // next version // Send the notification. // We use a layout id because it is a unique number. We use it later to cancel. mNM.notify((username + msg).hashCode(), mBuilder.build()); }
From source file:com.ibm.mobilefirstplatform.clientsdk.android.push.api.MFPPushIntentService.java
private void saveInSharedPreferences(MFPInternalPushMessage message) { SharedPreferences sharedPreferences = getSharedPreferences(MFPPush.PREFS_NAME, Context.MODE_PRIVATE); String msgString = message.toJson().toString(); //PREFS_NOTIFICATION_COUNT value provides the count of number of undelivered notifications stored in the sharedpreferences int count = sharedPreferences.getInt(MFPPush.PREFS_NOTIFICATION_COUNT, 0); //Increment the count and use it for the next notification count++;/*from w w w .ja va 2s . c o m*/ MFPPushUtils.storeContentInSharedPreferences(sharedPreferences, MFPPush.PREFS_NOTIFICATION_MSG + count, msgString); MFPPushUtils.storeContentInSharedPreferences(sharedPreferences, MFPPush.PREFS_NOTIFICATION_COUNT, count); }