Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b94a66bb authored by lijilou's avatar lijilou Committed by Jilou li
Browse files

PackageWatchdog: ensure the opend resources are eventually closed

Bug: none
Flag: EXEMPT minor optimization
Change-Id: Ie8c02889db5d747ccfda124d84ff7f92a86dee75
parent 219a0d17
Loading
Loading
Loading
Loading
+30 −17
Original line number Original line Diff line number Diff line
@@ -1849,15 +1849,19 @@ public class PackageWatchdog {
            bootMitigationCounts.put(observer.name, observer.getBootMitigationCount());
            bootMitigationCounts.put(observer.name, observer.getBootMitigationCount());
        }
        }


        FileOutputStream fileStream = null;
        ObjectOutputStream objectStream = null;
        try {
        try {
            FileOutputStream fileStream = new FileOutputStream(new File(filePath));
            fileStream = new FileOutputStream(new File(filePath));
            ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
            objectStream = new ObjectOutputStream(fileStream);
            objectStream.writeObject(bootMitigationCounts);
            objectStream.writeObject(bootMitigationCounts);
            objectStream.flush();
            objectStream.flush();
            objectStream.close();
            fileStream.close();
        } catch (Exception e) {
        } catch (Exception e) {
            Slog.i(TAG, "Could not save observers metadata to file: " + e);
            Slog.i(TAG, "Could not save observers metadata to file: " + e);
            return;
        } finally {
            IoUtils.closeQuietly(objectStream);
            IoUtils.closeQuietly(fileStream);
        }
        }
    }
    }


@@ -2008,14 +2012,26 @@ public class PackageWatchdog {
        void readAllObserversBootMitigationCountIfNecessary(String filePath) {
        void readAllObserversBootMitigationCountIfNecessary(String filePath) {
            File metadataFile = new File(filePath);
            File metadataFile = new File(filePath);
            if (metadataFile.exists()) {
            if (metadataFile.exists()) {
                FileInputStream fileStream = null;
                ObjectInputStream objectStream = null;
                HashMap<String, Integer> bootMitigationCounts = null;
                try {
                try {
                    FileInputStream fileStream = new FileInputStream(metadataFile);
                    fileStream = new FileInputStream(metadataFile);
                    ObjectInputStream objectStream = new ObjectInputStream(fileStream);
                    objectStream = new ObjectInputStream(fileStream);
                    HashMap<String, Integer> bootMitigationCounts =
                    bootMitigationCounts =
                            (HashMap<String, Integer>) objectStream.readObject();
                            (HashMap<String, Integer>) objectStream.readObject();
                    objectStream.close();
                } catch (Exception e) {
                    fileStream.close();
                    Slog.i(TAG, "Could not read observer metadata file: " + e);
                    return;
                } finally {
                    IoUtils.closeQuietly(objectStream);
                    IoUtils.closeQuietly(fileStream);
                }


                if (bootMitigationCounts == null || bootMitigationCounts.isEmpty()) {
                    Slog.i(TAG, "No observer in metadata file");
                    return;
                }
                for (int i = 0; i < mAllObservers.size(); i++) {
                for (int i = 0; i < mAllObservers.size(); i++) {
                    final ObserverInternal observer = mAllObservers.valueAt(i);
                    final ObserverInternal observer = mAllObservers.valueAt(i);
                    if (bootMitigationCounts.containsKey(observer.name)) {
                    if (bootMitigationCounts.containsKey(observer.name)) {
@@ -2023,9 +2039,6 @@ public class PackageWatchdog {
                                bootMitigationCounts.get(observer.name));
                                bootMitigationCounts.get(observer.name));
                    }
                    }
                }
                }
                } catch (Exception e) {
                    Slog.i(TAG, "Could not read observer metadata file: " + e);
                }
            }
            }
        }
        }