packages/apps/Settings
Revisão | d2136f7e5da6d36a685f5bed2727f0abdafb70bf (tree) |
---|---|
Hora | 2009-08-19 11:32:06 |
Autor | Suchi Amalapurapu <asuchitra@goog...> |
Commiter | Suchi Amalapurapu |
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
@@ -27,6 +27,7 @@ import android.content.Context; | ||
27 | 27 | import android.content.DialogInterface; |
28 | 28 | import android.content.Intent; |
29 | 29 | import android.content.IntentFilter; |
30 | +import android.content.SharedPreferences; | |
30 | 31 | import android.content.pm.ApplicationInfo; |
31 | 32 | import android.content.pm.IPackageStatsObserver; |
32 | 33 | import android.content.pm.PackageManager; |
@@ -103,6 +104,8 @@ public class ManageApplications extends ListActivity implements | ||
103 | 104 | DialogInterface.OnClickListener { |
104 | 105 | // TAG for this activity |
105 | 106 | private static final String TAG = "ManageApplications"; |
107 | + private static final String PREFS_NAME = "ManageAppsInfo.prefs"; | |
108 | + private static final String PREF_DISABLE_CACHE = "disableCache"; | |
106 | 109 | |
107 | 110 | // Log information boolean |
108 | 111 | private boolean localLOGV = Config.LOGV || false; |
@@ -1533,7 +1536,7 @@ public class ManageApplications extends ListActivity implements | ||
1533 | 1536 | private static final String mFileCacheName="ManageAppsInfo.txt"; |
1534 | 1537 | private static final int FILE_BUFFER_SIZE = 1024; |
1535 | 1538 | 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; | |
1537 | 1540 | private Map<String, AppInfo> mAppPropCache = new HashMap<String, AppInfo>(); |
1538 | 1541 | |
1539 | 1542 | private boolean isEmpty() { |
@@ -1581,6 +1584,10 @@ public class ManageApplications extends ListActivity implements | ||
1581 | 1584 | while(fis.available() > 0) { |
1582 | 1585 | fis.read(lenBytes, 0, 2); |
1583 | 1586 | int buffLen = (lenBytes[0] << 8) | lenBytes[1]; |
1587 | + if ((buffLen <= 0) || (buffLen > byteBuff.length)) { | |
1588 | + err = true; | |
1589 | + break; | |
1590 | + } | |
1584 | 1591 | // Buffer length cannot be great then max. |
1585 | 1592 | fis.read(byteBuff, 0, buffLen); |
1586 | 1593 | String buffStr = new String(byteBuff); |
@@ -1617,17 +1624,18 @@ public class ManageApplications extends ListActivity implements | ||
1617 | 1624 | fis.close(); |
1618 | 1625 | } catch (IOException e) { |
1619 | 1626 | Log.w(TAG, "Failed to close file " + cacheFile + " with exception : " +e); |
1627 | + err = true; | |
1620 | 1628 | } |
1621 | 1629 | } |
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 | + } | |
1627 | 1635 | } |
1628 | 1636 | } |
1629 | 1637 | |
1630 | - void writeToFile() { | |
1638 | + boolean writeToFile() { | |
1631 | 1639 | File cacheFile = new File(getFilesDir(), mFileCacheName); |
1632 | 1640 | FileOutputStream fos = null; |
1633 | 1641 | try { |
@@ -1662,34 +1670,58 @@ public class ManageApplications extends ListActivity implements | ||
1662 | 1670 | fos.write(byteBuff, 0, len); |
1663 | 1671 | } catch (IOException e) { |
1664 | 1672 | Log.w(TAG, "Failed to write to file : " + cacheFile + " with exception : " + e); |
1673 | + return false; | |
1665 | 1674 | } |
1666 | 1675 | } |
1667 | 1676 | if (DEBUG_CACHE_TIME) { |
1668 | 1677 | Log.i(TAG, "Took " + (SystemClock.uptimeMillis() - opStartTime) + " ms to write and process from file"); |
1669 | 1678 | } |
1679 | + return true; | |
1670 | 1680 | } catch (FileNotFoundException e) { |
1671 | 1681 | Log.w(TAG, "Error opening file for write operation : " + cacheFile+ |
1672 | 1682 | " with exception : " + e); |
1683 | + return false; | |
1673 | 1684 | } finally { |
1674 | 1685 | if (fos != null) { |
1675 | 1686 | try { |
1676 | 1687 | fos.close(); |
1677 | 1688 | } catch (IOException e) { |
1678 | 1689 | Log.w(TAG, "Failed closing file : " + cacheFile + " with exception : " + e); |
1690 | + return false; | |
1679 | 1691 | } |
1680 | 1692 | } |
1681 | 1693 | } |
1682 | 1694 | } |
1683 | 1695 | 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) { | |
1685 | 1705 | readFromFile(); |
1706 | + // Enable cache since the file has been read successfully | |
1707 | + editor.putBoolean(PREF_DISABLE_CACHE, false); | |
1708 | + editor.commit(); | |
1686 | 1709 | } |
1687 | 1710 | } |
1688 | 1711 | |
1689 | 1712 | 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(); | |
1690 | 1717 | if (FILE_CACHE) { |
1691 | - writeToFile(); | |
1718 | + boolean writeStatus = writeToFile(); | |
1692 | 1719 | 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 | + } | |
1693 | 1725 | } |
1694 | 1726 | } |
1695 | 1727 | } |