List of usage examples for android.content SharedPreferences getLong
long getLong(String key, long defValue);
From source file:org.videolan.vlc.gui.video.VideoPlayerActivity.java
/** * External extras:/*from w w w. j a v a2 s . c o m*/ * - position (long) - position of the video to start with (in ms) */ @SuppressWarnings({ "deprecation", "unchecked" }) private void load() { mLocation = null; String title = getResources().getString(R.string.title); boolean dontParse = false; boolean fromStart = false; String itemTitle = null; long intentPosition = -1; // position passed in by intent (ms) boolean useStun = false; if (getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_VIEW)) { /* Started from external application */ if (getIntent().getData() != null && getIntent().getData().getScheme() != null && getIntent().getData().getScheme().equals("content")) { if (getIntent().getData().getHost().equals("media")) { // Media URI Cursor cursor = managedQuery(getIntent().getData(), new String[] { MediaColumns.DATA }, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA); if (cursor.moveToFirst()) mLocation = LibVLC.PathToURI(cursor.getString(column_index)); } else { // other content-based URI (probably file pickers) mLocation = getIntent().getData().getPath(); } } else { // Plain URI mLocation = getIntent().getDataString(); } if (getIntent().getExtras() != null) intentPosition = getIntent().getExtras().getLong("position", -1); } else if (getIntent().getAction() != null && getIntent().getAction().equals(PLAY_FROM_VIDEOGRID) && getIntent().getExtras() != null) { /* Started from VideoListActivity */ mLocation = getIntent().getExtras().getString("itemLocation"); itemTitle = getIntent().getExtras().getString("itemTitle"); dontParse = getIntent().getExtras().getBoolean("dontParse"); fromStart = getIntent().getExtras().getBoolean("fromStart"); useStun = getIntent().getExtras().getBoolean("useStun"); } mSurface.setKeepScreenOn(true); /* Start / resume playback */ if (savedIndexPosition > -1) { mLibVLC.playIndex(savedIndexPosition); } else if (mLocation != null && mLocation.length() > 0 && !dontParse) { if (useStun) { mLibVLC.setStunServerAddr("192.168.36.124"); mLibVLC.setStunServerPort(3478); savedIndexPosition = mLibVLC.readMediaUseStun(mLocation, false); } else { savedIndexPosition = mLibVLC.readMedia(mLocation, false); } } if (mLocation != null && mLocation.length() > 0 && !dontParse) { // restore last position SharedPreferences preferences = getSharedPreferences(PreferencesActivity.NAME, MODE_PRIVATE); Media media = MediaDatabase.getInstance(this).getMedia(this, mLocation); if (media != null) { // in media library if (media.getTime() > 0 && !fromStart) mLibVLC.setTime(media.getTime()); mLastAudioTrack = media.getAudioTrack(); mLastSpuTrack = media.getSpuTrack(); } else { // not in media library long rTime = preferences.getLong(PreferencesActivity.VIDEO_RESUME_TIME, -1); SharedPreferences.Editor editor = preferences.edit(); editor.putLong(PreferencesActivity.VIDEO_RESUME_TIME, -1); editor.commit(); if (rTime > 0) mLibVLC.setTime(rTime); if (intentPosition > 0) mLibVLC.setTime(intentPosition); } String subtitleList_serialized = preferences.getString(PreferencesActivity.VIDEO_SUBTITLE_FILES, null); ArrayList<String> prefsList = new ArrayList<String>(); if (subtitleList_serialized != null) { ByteArrayInputStream bis = new ByteArrayInputStream(subtitleList_serialized.getBytes()); try { ObjectInputStream ois = new ObjectInputStream(bis); prefsList = (ArrayList<String>) ois.readObject(); } catch (ClassNotFoundException e) { } catch (StreamCorruptedException e) { } catch (IOException e) { } } for (String x : prefsList) { if (!mSubtitleSelectedFiles.contains(x)) mSubtitleSelectedFiles.add(x); } try { title = URLDecoder.decode(mLocation, "UTF-8"); } catch (UnsupportedEncodingException e) { } catch (IllegalArgumentException e) { } if (title.startsWith("file:")) { title = new File(title).getName(); int dotIndex = title.lastIndexOf('.'); if (dotIndex != -1) title = title.substring(0, dotIndex); } } else if (itemTitle != null) { title = itemTitle; } mTitle.setText(title); }
From source file:com.jtschohl.androidfirewall.MainActivity.java
public void toggleUser() { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = prefs.edit(); boolean multiuserenabled = getApplicationContext().getSharedPreferences(Api.PREFS_NAME, 0) .getBoolean(Api.PREF_MULTIUSER, false); if (prefs.getLong("userID", 0) == 0) { Log.d(TAG, "userHandle is 0"); editor.putString("chainName", "droidwall"); editor.commit();//from w w w . j av a 2s. co m Log.d(TAG, "User = " + prefs.getLong("userID", 0) + " and CHAINNAME = " + prefs.getString("chainName", "")); } else { Log.d(TAG, "userHandle greater than 0"); if (multiuserenabled) { editor.putString("chainName", prefs.getLong("userID", 0) + "droidwall"); editor.commit(); Log.d(TAG, "User = " + prefs.getLong("userID", 0) + " and CHAINNAME = " + prefs.getString("chainName", "")); } else { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.multiuser_title); builder.setMessage(R.string.multiuser_disabled); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { userSettings(); } }); AlertDialog dialog = builder.show(); TextView msg = (TextView) dialog.findViewById(android.R.id.message); msg.setGravity(Gravity.CENTER); } } }
From source file:com.android.mms.ui.MessageUtils.java
public static boolean checkAppSettingsNeedNotify(Context context) { boolean appNotificationEnabled = true; long appMute = 0; long appMuteStart = 0; /// M: check app settings SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); appNotificationEnabled = prefs.getBoolean(NotificationPreferenceActivity.NOTIFICATION_ENABLED, true); if (!appNotificationEnabled) { MmsLog.d(TAG, "app notification set disabled!"); return false; }/* w ww. j a v a 2 s. com*/ String muteStr = prefs.getString(NotificationPreferenceActivity.NOTIFICATION_MUTE, Integer.toString(0)); appMute = Integer.parseInt(muteStr); appMuteStart = prefs.getLong(NotificationPreferenceActivity.MUTE_START, 0); MmsLog.d(TAG, "\t appmute:" + appMute + ", appMute*3600=" + appMute * 3600 + "\t appMuteStart" + appMuteStart / 1000); if (appMuteStart > 0 && appMute > 0) { long currentTime = (System.currentTimeMillis() / 1000); MmsLog.d(TAG, "\t currentTime" + currentTime); if ((appMute * 3600 + appMuteStart / 1000) <= currentTime) { MmsLog.d(TAG, "thread mute timeout, reset to default."); appMute = 0; appMuteStart = 0; SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit(); editor.putLong(NotificationPreferenceActivity.MUTE_START, 0); editor.putString(NotificationPreferenceActivity.NOTIFICATION_MUTE, String.valueOf(appMute)); editor.apply(); } } if (appMute > 0) { return false; } return true; }
From source file:be.brunoparmentier.openbikesharing.app.activities.MapActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); getActionBar().setDisplayHomeAsUpEnabled(true); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); stationsDataSource = new StationsDataSource(this); ArrayList<Station> stations = stationsDataSource.getStations(); map = (MapView) findViewById(R.id.mapView); stationMarkerInfoWindow = new StationMarkerInfoWindow(R.layout.bonuspack_bubble, map); /* handling map events */ MapEventsOverlay mapEventsOverlay = new MapEventsOverlay(this, this); map.getOverlays().add(0, mapEventsOverlay); /* markers list */ GridMarkerClusterer stationsMarkers = new GridMarkerClusterer(this); Drawable clusterIconD = getResources().getDrawable(R.drawable.marker_cluster); Bitmap clusterIcon = ((BitmapDrawable) clusterIconD).getBitmap(); map.getOverlays().add(stationsMarkers); stationsMarkers.setIcon(clusterIcon); stationsMarkers.setGridSize(100);//from w w w .j a v a 2 s . com for (final Station station : stations) { stationsMarkers.add(createStationMarker(station)); } map.invalidate(); mapController = map.getController(); mapController.setZoom(16); map.setMultiTouchControls(true); map.setBuiltInZoomControls(true); map.setMinZoomLevel(3); /* map tile source */ String mapLayer = settings.getString("pref_map_layer", ""); switch (mapLayer) { case "mapnik": map.setTileSource(TileSourceFactory.MAPNIK); break; case "cyclemap": map.setTileSource(TileSourceFactory.CYCLEMAP); break; case "osmpublictransport": map.setTileSource(TileSourceFactory.PUBLIC_TRANSPORT); break; case "mapquestosm": map.setTileSource(TileSourceFactory.MAPQUESTOSM); break; default: map.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE); break; } GpsMyLocationProvider imlp = new GpsMyLocationProvider(this.getBaseContext()); imlp.setLocationUpdateMinDistance(1000); imlp.setLocationUpdateMinTime(60000); myLocationOverlay = new MyLocationNewOverlay(this.getBaseContext(), imlp, this.map); map.getOverlays().add(this.myLocationOverlay); myLocationOverlay.enableMyLocation(); try { LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); GeoPoint userLocation = new GeoPoint( locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)); mapController.animateTo(userLocation); } catch (NullPointerException e) { mapController.setZoom(13); double bikeNetworkLatitude = Double.longBitsToDouble(settings.getLong(PREF_KEY_NETWORK_LATITUDE, 0)); double bikeNetworkLongitude = Double.longBitsToDouble(settings.getLong(PREF_KEY_NETWORK_LONGITUDE, 0)); mapController.animateTo(new GeoPoint(bikeNetworkLatitude, bikeNetworkLongitude)); Toast.makeText(this, R.string.location_not_found, Toast.LENGTH_LONG).show(); } }
From source file:com.ichi2.anki2.DeckPicker.java
private void showStartupScreensAndDialogs(SharedPreferences preferences, int skip) { if (skip < 1 && preferences.getLong("lastTimeOpened", 0) == 0) { Intent infoIntent = new Intent(this, Info.class); infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_WELCOME); startActivityForResult(infoIntent, SHOW_INFO_WELCOME); if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT); }//ww w . j a va 2s . c o m } else if (skip < 2 && !preferences.getString("lastVersion", "").equals(AnkiDroidApp.getPkgVersion())) { preferences.edit().putBoolean("showBroadcastMessageToday", true).commit(); Intent infoIntent = new Intent(this, Info.class); infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_NEW_VERSION); startActivityForResult(infoIntent, SHOW_INFO_NEW_VERSION); if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT); } } else if (skip < 3 && upgradeNeeded()) { AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).edit() .putString("lastUpgradeVersion", AnkiDroidApp.getPkgVersion()).commit(); showUpgradeScreen(skip != 0, Info.UPGRADE_SCREEN_BASIC1); } else if (skip < 4 && hasErrorFiles()) { Intent i = new Intent(this, Feedback.class); startActivityForResult(i, REPORT_ERROR); if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT); } } else if (!AnkiDroidApp.isSdCardMounted()) { showDialog(DIALOG_SD_CARD_NOT_MOUNTED); } else if (!BackupManager.enoughDiscSpace(mPrefDeckPath)) {// && !preferences.getBoolean("dontShowLowMemory", // false)) { showDialog(DIALOG_NO_SPACE_LEFT); } else if (preferences.getBoolean("noSpaceLeft", false)) { showDialog(DIALOG_BACKUP_NO_SPACE_LEFT); preferences.edit().putBoolean("noSpaceLeft", false).commit(); } else if (mImportPath != null && AnkiDroidApp.colIsOpen()) { showDialog(DIALOG_IMPORT); } else { // if the collection is empty, user has already upgraded the app but maybe did not successfully upgrade the decks if (!preferences.getString("lastUpgradeVersion", "").equals(AnkiDroidApp.getPkgVersion()) && (new File(AnkiDroidApp.getCurrentAnkiDroidDirectory()) .listFiles(new OldAnkiDeckFilter()).length) > 0) { StyledDialog.Builder builder = new StyledDialog.Builder(DeckPicker.this); builder.setTitle(R.string.deck_upgrade_title); builder.setIcon(R.drawable.ic_dialog_alert); builder.setMessage(R.string.deck_upgrade_already_upgraded); builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { restartUpgradeProcess(); } }); builder.setCancelable(false); builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).edit() .putString("lastUpgradeVersion", AnkiDroidApp.getPkgVersion()).commit(); loadCollection(); } }); builder.show(); } else { loadCollection(); } } }
From source file:com.nit.vicky.DeckPicker.java
private void showStartupScreensAndDialogs(SharedPreferences preferences, int skip) { if (skip < 1 && preferences.getLong("lastTimeOpened", 0) == 0) { Intent infoIntent = new Intent(this, Info.class); infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_WELCOME); startActivityForResult(infoIntent, SHOW_INFO_WELCOME); if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT); }/*www . j av a2s .co m*/ } else if (skip < 2 && !preferences.getString("lastVersion", "").equals(AnkiDroidApp.getPkgVersionName())) { preferences.edit().putBoolean("showBroadcastMessageToday", true).commit(); Intent infoIntent = new Intent(this, Info.class); infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_NEW_VERSION); startActivityForResult(infoIntent, SHOW_INFO_NEW_VERSION); if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT); } } else if (skip < 3 && upgradeNeeded()) { // Note that the "upgrade needed" refers to upgrading Anki 1.x decks, not to newer // versions of AnkiDroid. AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()).edit() .putInt("lastUpgradeVersion", AnkiDroidApp.getPkgVersionCode()).commit(); showUpgradeScreen(skip != 0, Info.UPGRADE_SCREEN_BASIC1); } else if (skip < 4 && hasErrorFiles()) { Intent i = new Intent(this, Feedback.class); startActivityForResult(i, REPORT_ERROR); if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT); } } else if (!AnkiDroidApp.isSdCardMounted()) { showDialog(DIALOG_SD_CARD_NOT_MOUNTED); } else if (!BackupManager.enoughDiscSpace(mPrefDeckPath)) {// && !preferences.getBoolean("dontShowLowMemory", // false)) { showDialog(DIALOG_NO_SPACE_LEFT); } else if (preferences.getBoolean("noSpaceLeft", false)) { showDialog(DIALOG_BACKUP_NO_SPACE_LEFT); preferences.edit().putBoolean("noSpaceLeft", false).commit(); } else if (mImportPath != null && AnkiDroidApp.colIsOpen()) { showDialog(DIALOG_IMPORT); } else { // AnkiDroid is being updated and a collection already exists. We check if we are upgrading // to a version that contains additions to the database integrity check routine that we would // like to run on all collections. A missing version number is assumed to be a fresh // installation of AnkiDroid and we don't run the check. int current = AnkiDroidApp.getPkgVersionCode(); int previous; if (!preferences.contains("lastUpgradeVersion")) { // Fresh install previous = current; } else { try { previous = preferences.getInt("lastUpgradeVersion", current); } catch (ClassCastException e) { // Previous versions stored this as a string. String s = preferences.getString("lastUpgradeVersion", ""); // The last version of AnkiDroid that stored this as a string was 2.0.2. // We manually set the version here, but anything older will force a DB // check. if (s.equals("2.0.2")) { previous = 40; } else { previous = 0; } } } preferences.edit().putInt("lastUpgradeVersion", current).commit(); if (previous < AnkiDroidApp.CHECK_DB_AT_VERSION) { DeckTask.launchDeckTask(DeckTask.TASK_TYPE_OPEN_COLLECTION, new Listener() { @Override public void onPostExecute(DeckTask task, TaskData result) { mOpenCollectionHandler.onPostExecute(result); integrityCheck(); } @Override public void onPreExecute(DeckTask task) { } @Override public void onProgressUpdate(DeckTask task, TaskData... values) { } }, new DeckTask.TaskData(AnkiDroidApp.getCollectionPath())); } else { loadCollection(); } } }
From source file:de.vanita5.twittnuker.util.Utils.java
public static long getDefaultAccountId(final Context context) { if (context == null) return -1; final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); return prefs.getLong(KEY_DEFAULT_ACCOUNT_ID, -1); }
From source file:com.hichinaschool.flashcards.anki.DeckPicker.java
private void showStartupScreensAndDialogs(SharedPreferences preferences, int skip) { if (skip < 1 && preferences.getLong("lastTimeOpened", 0) == 0) { showStartupScreensAndDialogs(preferences, 2); // Intent infoIntent = new Intent(this, Info.class); // infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_WELCOME); // startActivityForResult(infoIntent, SHOW_INFO_WELCOME); // if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) { // ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT); // }//ww w .j a v a 2 s .co m } else if (skip < 2 && !preferences.getString("lastVersion", "").equals(AnkiDroidApp.getPkgVersionName())) { showStartupScreensAndDialogs(preferences, 2); // preferences.edit().putBoolean("showBroadcastMessageToday", true).commit(); // Intent infoIntent = new Intent(this, Info.class); // infoIntent.putExtra(Info.TYPE_EXTRA, Info.TYPE_NEW_VERSION); // startActivityForResult(infoIntent, SHOW_INFO_NEW_VERSION); // if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) { // ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT); // } } else if (skip < 3 && upgradeNeeded()) { // Note that the "upgrade needed" refers to upgrading Anki 1.x decks, not to newer // versions of AnkiDroid. // AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext()) // .edit().putInt("lastUpgradeVersion", AnkiDroidApp.getPkgVersionCode()).commit(); // showUpgradeScreen(skip != 0, Info.UPGRADE_SCREEN_BASIC1); showStartupScreensAndDialogs(preferences, 2); } else if (skip < 4 && hasErrorFiles()) { showStartupScreensAndDialogs(preferences, 2); // HiChina // Intent i = new Intent(this, Feedback.class); // startActivityForResult(i, REPORT_ERROR); // if (skip != 0 && AnkiDroidApp.SDK_VERSION > 4) { // ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.LEFT); // } } else if (!AnkiDroidApp.isSdCardMounted()) { showDialog(DIALOG_SD_CARD_NOT_MOUNTED); } else if (!BackupManager.enoughDiscSpace(mPrefDeckPath)) {// && !preferences.getBoolean("dontShowLowMemory", // false)) { showDialog(DIALOG_NO_SPACE_LEFT); } else if (preferences.getBoolean("noSpaceLeft", false)) { showDialog(DIALOG_BACKUP_NO_SPACE_LEFT); preferences.edit().putBoolean("noSpaceLeft", false).commit(); } else if (mImportPath != null && AnkiDroidApp.colIsOpen()) { showDialog(DIALOG_IMPORT); } else { // AnkiDroid is being updated and a collection already exists. We check if we are upgrading // to a version that contains additions to the database integrity check routine that we would // like to run on all collections. A missing version number is assumed to be a fresh // installation of AnkiDroid and we don't run the check. int current = AnkiDroidApp.getPkgVersionCode(); int previous; if (!preferences.contains("lastUpgradeVersion")) { // Fresh install previous = current; } else { try { previous = preferences.getInt("lastUpgradeVersion", current); } catch (ClassCastException e) { // Previous versions stored this as a string. String s = preferences.getString("lastUpgradeVersion", ""); // The last version of AnkiDroid that stored this as a string was 2.0.2. // We manually set the version here, but anything older will force a DB // check. if (s.equals("2.0.2")) { previous = 40; } else { previous = 0; } } } preferences.edit().putInt("lastUpgradeVersion", current).commit(); if (previous < AnkiDroidApp.CHECK_DB_AT_VERSION) { DeckTask.launchDeckTask(DeckTask.TASK_TYPE_OPEN_COLLECTION, new Listener() { @Override public void onPostExecute(DeckTask task, TaskData result) { mOpenCollectionHandler.onPostExecute(result); integrityCheck(); } @Override public void onPreExecute(DeckTask task) { } @Override public void onProgressUpdate(DeckTask task, TaskData... values) { } }, new DeckTask.TaskData(AnkiDroidApp.getCollectionPath())); } else { loadCollection(); } } }
From source file:com.hichinaschool.flashcards.anki.DeckPicker.java
private SharedPreferences restorePreferences() { SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext()); mPrefDeckPath = AnkiDroidApp.getCurrentAnkiDroidDirectory(); mLastTimeOpened = preferences.getLong("lastTimeOpened", 0); mSwipeEnabled = AnkiDroidApp.initiateGestures(this, preferences); // mInvertedColors = preferences.getBoolean("invertedColors", false); // mSwap = preferences.getBoolean("swapqa", false); // mLocale = preferences.getString("language", ""); // setLanguage(mLocale); return preferences; }
From source file:saphion.services.ForegroundService.java
private void calcStat(Status mStat, SharedPreferences mPrefs, Editor mPrefEditor, int diff, boolean b) { // Step 1: retrieve previous time // Step 2: calculate time difference // Step 3: scale it to 1% by dividing it with #diff // Step 4: take the previous average and multiply with count // Step 5: add the new time difference with result from previous step // Step 6: increment the count // Step 7: divide the Step 5 result with new count // Step 8: save the values if (b) {//from w ww. ja v a2s . c o m long prevAvg = mPrefs.getLong(PreferenceHelper.STAT_CHARGING_AVGTIME, 90); long count = mPrefs.getLong(PreferenceHelper.STAT_DISCONNECTED_COUNT, 0); long newAvg = prevAvg; // retrieve previous time String prevTime = mPrefs.getString(PreferenceHelper.STAT_CONNECTED_LAST_TIME, TimeFuncs.getCurrentTimeStamp()); // Calculate difference long timeDiff = TimeFuncs.newDiff(TimeFuncs.GetItemDate(prevTime), TimeFuncs.GetItemDate(TimeFuncs.getCurrentTimeStamp())); // scale to 1% by dividing by #diff if (diff != 0) { timeDiff = timeDiff / diff; // take the previous average and multiply with count prevAvg = prevAvg * count; // increment the count count = count + 1; // add the new time difference with result from previous // step // divide the Step 5 result with new count newAvg = (prevAvg + timeDiff) / (count); } mPrefEditor.putLong(PreferenceHelper.STAT_CHARGING_AVGTIME, newAvg); mPrefEditor.putLong(PreferenceHelper.STAT_DISCONNECTED_COUNT, count); mPrefEditor.putString(PreferenceHelper.STAT_DISCONNECTED_LAST_TIME, TimeFuncs.getCurrentTimeStamp()); mPrefEditor.putInt(PreferenceHelper.STAT_DISCONNECTED_LAST_LEVEL, mStat.getLevel()); } else { long prevAvg = mPrefs.getLong(PreferenceHelper.STAT_DISCHARGING_AVGTIME, 612); long count = mPrefs.getLong(PreferenceHelper.STAT_CONNECTED_COUNT, 0); long newAvg = prevAvg; // retrieve previous time String prevTime = mPrefs.getString(PreferenceHelper.STAT_DISCONNECTED_LAST_TIME, TimeFuncs.getCurrentTimeStamp()); // Calculate difference long timeDiff = TimeFuncs.newDiff(TimeFuncs.GetItemDate(prevTime), TimeFuncs.GetItemDate(TimeFuncs.getCurrentTimeStamp())); // scale to 1% by dividing by #diff if (diff != 0) { timeDiff = timeDiff / diff; // take the previous average and multiply with count prevAvg = prevAvg * count; // increment the count count = count + 1; // add the new time difference with result from previous // step // divide the Step 5 result with new count newAvg = (prevAvg + timeDiff) / (count); } mPrefEditor.putLong(PreferenceHelper.STAT_DISCHARGING_AVGTIME, newAvg); mPrefEditor.putLong(PreferenceHelper.STAT_CONNECTED_COUNT, count); mPrefEditor.putString(PreferenceHelper.STAT_CONNECTED_LAST_TIME, TimeFuncs.getCurrentTimeStamp()); mPrefEditor.putInt(PreferenceHelper.STAT_CONNECTED_LAST_LEVEL, mStat.getLevel()); } mPrefEditor.commit(); }