• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

packages/apps/Settings


Commit MetaInfo

Revisãod2136f7e5da6d36a685f5bed2727f0abdafb70bf (tree)
Hora2009-08-19 11:32:06
AutorSuchi Amalapurapu <asuchitra@goog...>
CommiterSuchi Amalapurapu

Mensagem de Log

Create a shared preference setting and use it before loading/unloading cache
and disable cache if file operations failed in the last launch.
A simple if check when reading buffer size from file

Mudança Sumário

Diff

--- a/src/com/android/settings/ManageApplications.java
+++ b/src/com/android/settings/ManageApplications.java
@@ -27,6 +27,7 @@ import android.content.Context;
2727 import android.content.DialogInterface;
2828 import android.content.Intent;
2929 import android.content.IntentFilter;
30+import android.content.SharedPreferences;
3031 import android.content.pm.ApplicationInfo;
3132 import android.content.pm.IPackageStatsObserver;
3233 import android.content.pm.PackageManager;
@@ -103,6 +104,8 @@ public class ManageApplications extends ListActivity implements
103104 DialogInterface.OnClickListener {
104105 // TAG for this activity
105106 private static final String TAG = "ManageApplications";
107+ private static final String PREFS_NAME = "ManageAppsInfo.prefs";
108+ private static final String PREF_DISABLE_CACHE = "disableCache";
106109
107110 // Log information boolean
108111 private boolean localLOGV = Config.LOGV || false;
@@ -1533,7 +1536,7 @@ public class ManageApplications extends ListActivity implements
15331536 private static final String mFileCacheName="ManageAppsInfo.txt";
15341537 private static final int FILE_BUFFER_SIZE = 1024;
15351538 private static final boolean DEBUG_CACHE = false;
1536- private static final boolean DEBUG_CACHE_TIME = true;
1539+ private static final boolean DEBUG_CACHE_TIME = false;
15371540 private Map<String, AppInfo> mAppPropCache = new HashMap<String, AppInfo>();
15381541
15391542 private boolean isEmpty() {
@@ -1581,6 +1584,10 @@ public class ManageApplications extends ListActivity implements
15811584 while(fis.available() > 0) {
15821585 fis.read(lenBytes, 0, 2);
15831586 int buffLen = (lenBytes[0] << 8) | lenBytes[1];
1587+ if ((buffLen <= 0) || (buffLen > byteBuff.length)) {
1588+ err = true;
1589+ break;
1590+ }
15841591 // Buffer length cannot be great then max.
15851592 fis.read(byteBuff, 0, buffLen);
15861593 String buffStr = new String(byteBuff);
@@ -1617,17 +1624,18 @@ public class ManageApplications extends ListActivity implements
16171624 fis.close();
16181625 } catch (IOException e) {
16191626 Log.w(TAG, "Failed to close file " + cacheFile + " with exception : " +e);
1627+ err = true;
16201628 }
16211629 }
1622- }
1623- if (err) {
1624- Log.i(TAG, "Failed to load cache. Not using cache for now.");
1625- // Clear cache and bail out
1626- mAppPropCache.clear();
1630+ if (err) {
1631+ Log.i(TAG, "Failed to load cache. Not using cache for now.");
1632+ // Clear cache and bail out
1633+ mAppPropCache.clear();
1634+ }
16271635 }
16281636 }
16291637
1630- void writeToFile() {
1638+ boolean writeToFile() {
16311639 File cacheFile = new File(getFilesDir(), mFileCacheName);
16321640 FileOutputStream fos = null;
16331641 try {
@@ -1662,34 +1670,58 @@ public class ManageApplications extends ListActivity implements
16621670 fos.write(byteBuff, 0, len);
16631671 } catch (IOException e) {
16641672 Log.w(TAG, "Failed to write to file : " + cacheFile + " with exception : " + e);
1673+ return false;
16651674 }
16661675 }
16671676 if (DEBUG_CACHE_TIME) {
16681677 Log.i(TAG, "Took " + (SystemClock.uptimeMillis() - opStartTime) + " ms to write and process from file");
16691678 }
1679+ return true;
16701680 } catch (FileNotFoundException e) {
16711681 Log.w(TAG, "Error opening file for write operation : " + cacheFile+
16721682 " with exception : " + e);
1683+ return false;
16731684 } finally {
16741685 if (fos != null) {
16751686 try {
16761687 fos.close();
16771688 } catch (IOException e) {
16781689 Log.w(TAG, "Failed closing file : " + cacheFile + " with exception : " + e);
1690+ return false;
16791691 }
16801692 }
16811693 }
16821694 }
16831695 private void loadCache() {
1684- if (FILE_CACHE) {
1696+ // Restore preferences
1697+ SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
1698+ boolean disable = settings.getBoolean(PREF_DISABLE_CACHE, true);
1699+ if (disable) Log.w(TAG, "Cache has been disabled");
1700+ // Disable cache till the data is loaded successfully
1701+ SharedPreferences.Editor editor = settings.edit();
1702+ editor.putBoolean(PREF_DISABLE_CACHE, true);
1703+ editor.commit();
1704+ if (FILE_CACHE && !disable) {
16851705 readFromFile();
1706+ // Enable cache since the file has been read successfully
1707+ editor.putBoolean(PREF_DISABLE_CACHE, false);
1708+ editor.commit();
16861709 }
16871710 }
16881711
16891712 private void updateCache() {
1713+ SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
1714+ SharedPreferences.Editor editor = settings.edit();
1715+ editor.putBoolean(PREF_DISABLE_CACHE, true);
1716+ editor.commit();
16901717 if (FILE_CACHE) {
1691- writeToFile();
1718+ boolean writeStatus = writeToFile();
16921719 mAppPropCache.clear();
1720+ if (writeStatus) {
1721+ // Enable cache since the file has been read successfully
1722+ editor.putBoolean(PREF_DISABLE_CACHE, false);
1723+ editor.commit();
1724+ }
16931725 }
16941726 }
16951727 }