Search in sources :

Example 21 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class UserManagerService method readApplicationRestrictionsLP.

@VisibleForTesting
static Bundle readApplicationRestrictionsLP(AtomicFile restrictionsFile) {
    final Bundle restrictions = new Bundle();
    final ArrayList<String> values = new ArrayList<>();
    if (!restrictionsFile.getBaseFile().exists()) {
        return restrictions;
    }
    FileInputStream fis = null;
    try {
        fis = restrictionsFile.openRead();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fis, StandardCharsets.UTF_8.name());
        XmlUtils.nextElement(parser);
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            Slog.e(LOG_TAG, "Unable to read restrictions file " + restrictionsFile.getBaseFile());
            return restrictions;
        }
        while (parser.next() != XmlPullParser.END_DOCUMENT) {
            readEntry(restrictions, values, parser);
        }
    } catch (IOException | XmlPullParserException e) {
        Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e);
    } finally {
        IoUtils.closeQuietly(fis);
    }
    return restrictions;
}
Also used : Bundle(android.os.Bundle) PersistableBundle(android.os.PersistableBundle) ArrayList(java.util.ArrayList) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 22 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class ShortcutService method openIconFileForWrite.

/**
     * Build the cached bitmap filename for a shortcut icon.
     *
     * The filename will be based on the ID, except certain characters will be escaped.
     */
@VisibleForTesting
FileOutputStreamWithPath openIconFileForWrite(@UserIdInt int userId, ShortcutInfo shortcut) throws IOException {
    final File packagePath = new File(getUserBitmapFilePath(userId), shortcut.getPackage());
    if (!packagePath.isDirectory()) {
        packagePath.mkdirs();
        if (!packagePath.isDirectory()) {
            throw new IOException("Unable to create directory " + packagePath);
        }
        SELinux.restorecon(packagePath);
    }
    final String baseName = String.valueOf(injectCurrentTimeMillis());
    for (int suffix = 0; ; suffix++) {
        final String filename = (suffix == 0 ? baseName : baseName + "_" + suffix) + ".png";
        final File file = new File(packagePath, filename);
        if (!file.exists()) {
            if (DEBUG) {
                Slog.d(TAG, "Saving icon to " + file.getAbsolutePath());
            }
            return new FileOutputStreamWithPath(file);
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 23 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class InputMethodUtils method getImplicitlyApplicableSubtypesLocked.

@VisibleForTesting
public static ArrayList<InputMethodSubtype> getImplicitlyApplicableSubtypesLocked(Resources res, InputMethodInfo imi) {
    final LocaleList systemLocales = res.getConfiguration().getLocales();
    synchronized (sCacheLock) {
        // it does not check if subtypes are also identical.
        if (systemLocales.equals(sCachedSystemLocales) && sCachedInputMethodInfo == imi) {
            return new ArrayList<>(sCachedResult);
        }
    }
    // Note: Only resource info in "res" is used in getImplicitlyApplicableSubtypesLockedImpl().
    // TODO: Refactor getImplicitlyApplicableSubtypesLockedImpl() so that it can receive
    // LocaleList rather than Resource.
    final ArrayList<InputMethodSubtype> result = getImplicitlyApplicableSubtypesLockedImpl(res, imi);
    synchronized (sCacheLock) {
        // Both LocaleList and InputMethodInfo are immutable. No need to copy them here.
        sCachedSystemLocales = systemLocales;
        sCachedInputMethodInfo = imi;
        sCachedResult = new ArrayList<>(result);
    }
    return result;
}
Also used : LocaleList(android.os.LocaleList) InputMethodSubtype(android.view.inputmethod.InputMethodSubtype) ArrayList(java.util.ArrayList) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 24 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsFactory method javaReadNetworkStatsDetail.

/**
     * Parse and return {@link NetworkStats} with UID-level details. Values are
     * expected to monotonically increase since device boot.
     */
@VisibleForTesting
public static NetworkStats javaReadNetworkStatsDetail(File detailPath, int limitUid, String[] limitIfaces, int limitTag) throws IOException {
    final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
    final NetworkStats stats = new NetworkStats(SystemClock.elapsedRealtime(), 24);
    final NetworkStats.Entry entry = new NetworkStats.Entry();
    int idx = 1;
    int lastIdx = 1;
    ProcFileReader reader = null;
    try {
        // open and consume header line
        reader = new ProcFileReader(new FileInputStream(detailPath));
        reader.finishLine();
        while (reader.hasMoreData()) {
            idx = reader.nextInt();
            if (idx != lastIdx + 1) {
                throw new ProtocolException("inconsistent idx=" + idx + " after lastIdx=" + lastIdx);
            }
            lastIdx = idx;
            entry.iface = reader.nextString();
            entry.tag = kernelToTag(reader.nextString());
            entry.uid = reader.nextInt();
            entry.set = reader.nextInt();
            entry.rxBytes = reader.nextLong();
            entry.rxPackets = reader.nextLong();
            entry.txBytes = reader.nextLong();
            entry.txPackets = reader.nextLong();
            if ((limitIfaces == null || ArrayUtils.contains(limitIfaces, entry.iface)) && (limitUid == UID_ALL || limitUid == entry.uid) && (limitTag == TAG_ALL || limitTag == entry.tag)) {
                stats.addValues(entry);
            }
            reader.finishLine();
        }
    } catch (NullPointerException e) {
        throw new ProtocolException("problem parsing idx " + idx, e);
    } catch (NumberFormatException e) {
        throw new ProtocolException("problem parsing idx " + idx, e);
    } finally {
        IoUtils.closeQuietly(reader);
        StrictMode.setThreadPolicy(savedPolicy);
    }
    return stats;
}
Also used : StrictMode(android.os.StrictMode) ProcFileReader(com.android.internal.util.ProcFileReader) ProtocolException(java.net.ProtocolException) NetworkStats(android.net.NetworkStats) FileInputStream(java.io.FileInputStream) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 25 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.

the class AccountManagerService method getPreNDatabaseName.

@VisibleForTesting
String getPreNDatabaseName(int userId) {
    File systemDir = Environment.getDataSystemDirectory();
    File databaseFile = new File(Environment.getUserSystemDirectory(userId), PRE_N_DATABASE_NAME);
    if (userId == 0) {
        // Migrate old file, if it exists, to the new location.
        // Make sure the new file doesn't already exist. A dummy file could have been
        // accidentally created in the old location, causing the new one to become corrupted
        // as well.
        File oldFile = new File(systemDir, PRE_N_DATABASE_NAME);
        if (oldFile.exists() && !databaseFile.exists()) {
            // Check for use directory; create if it doesn't exist, else renameTo will fail
            File userDir = Environment.getUserSystemDirectory(userId);
            if (!userDir.exists()) {
                if (!userDir.mkdirs()) {
                    throw new IllegalStateException("User dir cannot be created: " + userDir);
                }
            }
            if (!oldFile.renameTo(databaseFile)) {
                throw new IllegalStateException("User dir cannot be migrated: " + databaseFile);
            }
        }
    }
    return databaseFile.getPath();
}
Also used : File(java.io.File) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.android.internal.annotations.VisibleForTesting)141 ArrayList (java.util.ArrayList)39 IOException (java.io.IOException)26 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)18 ComponentName (android.content.ComponentName)15 File (java.io.File)14 RemoteException (android.os.RemoteException)13 ArraySet (android.util.ArraySet)10 AtomicFile (android.util.AtomicFile)10 FileInputStream (java.io.FileInputStream)10 Locale (java.util.Locale)10 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)9 FileNotFoundException (java.io.FileNotFoundException)9 Notification (android.app.Notification)6 ErrnoException (android.system.ErrnoException)6 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)6 FileOutputStream (java.io.FileOutputStream)6 XmlSerializer (org.xmlpull.v1.XmlSerializer)6 ITransientNotification (android.app.ITransientNotification)5 UserInfo (android.content.pm.UserInfo)5