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

Commit 592611bd authored by Alexei Nicoara's avatar Alexei Nicoara Committed by Android (Google) Code Review
Browse files

Merge "PackageWatchdog: ensure the opend resources are eventually closed" into main

parents 3f5ead49 4cee9db7
Loading
Loading
Loading
Loading
+30 −17
Original line number Diff line number Diff line
@@ -2074,15 +2074,19 @@ public class PackageWatchdog {
            bootMitigationCounts.put(observer.name, observer.getBootMitigationCount());
        }

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

@@ -2233,14 +2237,26 @@ public class PackageWatchdog {
        void readAllObserversBootMitigationCountIfNecessary(String filePath) {
            File metadataFile = new File(filePath);
            if (metadataFile.exists()) {
                FileInputStream fileStream = null;
                ObjectInputStream objectStream = null;
                HashMap<String, Integer> bootMitigationCounts = null;
                try {
                    FileInputStream fileStream = new FileInputStream(metadataFile);
                    ObjectInputStream objectStream = new ObjectInputStream(fileStream);
                    HashMap<String, Integer> bootMitigationCounts =
                    fileStream = new FileInputStream(metadataFile);
                    objectStream = new ObjectInputStream(fileStream);
                    bootMitigationCounts =
                            (HashMap<String, Integer>) objectStream.readObject();
                    objectStream.close();
                    fileStream.close();
                } catch (Exception e) {
                    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++) {
                    final ObserverInternal observer = mAllObservers.valueAt(i);
                    if (bootMitigationCounts.containsKey(observer.name)) {
@@ -2248,9 +2264,6 @@ public class PackageWatchdog {
                                bootMitigationCounts.get(observer.name));
                    }
                }
                } catch (Exception e) {
                    Slog.i(TAG, "Could not read observer metadata file: " + e);
                }
            }
        }
    }
+30 −17
Original line number Diff line number Diff line
@@ -2021,15 +2021,19 @@ public class PackageWatchdog {
            bootMitigationCounts.put(observer.name, observer.getBootMitigationCount());
        }

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

@@ -2180,14 +2184,26 @@ public class PackageWatchdog {
        void readAllObserversBootMitigationCountIfNecessary(String filePath) {
            File metadataFile = new File(filePath);
            if (metadataFile.exists()) {
                FileInputStream fileStream = null;
                ObjectInputStream objectStream = null;
                HashMap<String, Integer> bootMitigationCounts = null;
                try {
                    FileInputStream fileStream = new FileInputStream(metadataFile);
                    ObjectInputStream objectStream = new ObjectInputStream(fileStream);
                    HashMap<String, Integer> bootMitigationCounts =
                    fileStream = new FileInputStream(metadataFile);
                    objectStream = new ObjectInputStream(fileStream);
                    bootMitigationCounts =
                            (HashMap<String, Integer>) objectStream.readObject();
                    objectStream.close();
                    fileStream.close();
                } catch (Exception e) {
                    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++) {
                    final ObserverInternal observer = mAllObservers.valueAt(i);
                    if (bootMitigationCounts.containsKey(observer.name)) {
@@ -2195,9 +2211,6 @@ public class PackageWatchdog {
                                bootMitigationCounts.get(observer.name));
                    }
                }
                } catch (Exception e) {
                    Slog.i(TAG, "Could not read observer metadata file: " + e);
                }
            }
        }
    }