diff --git a/src/com/android/settings/LicenseHtmlGeneratorFromXml.java b/src/com/android/settings/LicenseHtmlGeneratorFromXml.java deleted file mode 100644 index 7025c5adf3f..00000000000 --- a/src/com/android/settings/LicenseHtmlGeneratorFromXml.java +++ /dev/null @@ -1,292 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings; - -import android.support.annotation.VisibleForTesting; -import android.text.TextUtils; -import android.util.Log; -import android.util.Xml; - -import org.xmlpull.v1.XmlPullParser; -import org.xmlpull.v1.XmlPullParserException; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.zip.GZIPInputStream; - -/** - * The utility class that generate a license html file from xml files. - * All the HTML snippets and logic are copied from build/make/tools/generate-notice-files.py. - * - * TODO: Remove duplicate codes once backward support ends. - */ -class LicenseHtmlGeneratorFromXml { - private static final String TAG = "LicenseHtmlGeneratorFromXml"; - - private static final String TAG_ROOT = "licenses"; - private static final String TAG_FILE_NAME = "file-name"; - private static final String TAG_FILE_CONTENT = "file-content"; - private static final String ATTR_CONTENT_ID = "contentId"; - - private static final String HTML_HEAD_STRING = - "\n" + - "\n" + - "" + - "\n" + - "
\n" + - "\n" + - "
\n" + - ""; - - private static final String HTML_REAR_STRING = - "
"; - - private final List mXmlFiles; - - /* - * A map from a file name to a content id (MD5 sum of file content) for its license. - * For example, "/system/priv-app/TeleService/TeleService.apk" maps to - * "9645f39e9db895a4aa6e02cb57294595". Here "9645f39e9db895a4aa6e02cb57294595" is a MD5 sum - * of the content of packages/services/Telephony/MODULE_LICENSE_APACHE2. - */ - private final Map mFileNameToContentIdMap = new HashMap(); - - /* - * A map from a content id (MD5 sum of file content) to a license file content. - * For example, "9645f39e9db895a4aa6e02cb57294595" maps to the content string of - * packages/services/Telephony/MODULE_LICENSE_APACHE2. Here "9645f39e9db895a4aa6e02cb57294595" - * is a MD5 sum of the file content. - */ - private final Map mContentIdToFileContentMap = new HashMap(); - - static class ContentIdAndFileNames { - final String mContentId; - final List mFileNameList = new ArrayList(); - - ContentIdAndFileNames(String contentId) { - mContentId = contentId; - } - } - - private LicenseHtmlGeneratorFromXml(List xmlFiles) { - mXmlFiles = xmlFiles; - } - - public static boolean generateHtml(List xmlFiles, File outputFile) { - LicenseHtmlGeneratorFromXml genertor = new LicenseHtmlGeneratorFromXml(xmlFiles); - return genertor.generateHtml(outputFile); - } - - private boolean generateHtml(File outputFile) { - for (File xmlFile : mXmlFiles) { - parse(xmlFile); - } - - if (mFileNameToContentIdMap.isEmpty() || mContentIdToFileContentMap.isEmpty()) { - return false; - } - - PrintWriter writer = null; - try { - writer = new PrintWriter(outputFile); - - generateHtml(mFileNameToContentIdMap, mContentIdToFileContentMap, writer); - - writer.flush(); - writer.close(); - return true; - } catch (FileNotFoundException | SecurityException e) { - Log.e(TAG, "Failed to generate " + outputFile, e); - - if (writer != null) { - writer.close(); - } - return false; - } - } - - private void parse(File xmlFile) { - if (xmlFile == null || !xmlFile.exists() || xmlFile.length() == 0) { - return; - } - - InputStreamReader in = null; - try { - if (xmlFile.getName().endsWith(".gz")) { - in = new InputStreamReader(new GZIPInputStream(new FileInputStream(xmlFile))); - } else { - in = new FileReader(xmlFile); - } - - parse(in, mFileNameToContentIdMap, mContentIdToFileContentMap); - - in.close(); - } catch (XmlPullParserException | IOException e) { - Log.e(TAG, "Failed to parse " + xmlFile, e); - if (in != null) { - try { - in.close(); - } catch (IOException ie) { - Log.w(TAG, "Failed to close " + xmlFile); - } - } - } - } - - /* - * Parses an input stream and fills a map from a file name to a content id for its license - * and a map from a content id to a license file content. - * - * Following xml format is expected from the input stream. - * - * - * file1 - * file2 - * ... - * license1 file contents - * license2 file contents - * ... - * - */ - @VisibleForTesting - static void parse(InputStreamReader in, Map outFileNameToContentIdMap, - Map outContentIdToFileContentMap) - throws XmlPullParserException, IOException { - Map fileNameToContentIdMap = new HashMap(); - Map contentIdToFileContentMap = new HashMap(); - - XmlPullParser parser = Xml.newPullParser(); - parser.setInput(in); - parser.nextTag(); - - parser.require(XmlPullParser.START_TAG, "", TAG_ROOT); - - int state = parser.getEventType(); - while (state != XmlPullParser.END_DOCUMENT) { - if (state == XmlPullParser.START_TAG) { - if (TAG_FILE_NAME.equals(parser.getName())) { - String contentId = parser.getAttributeValue("", ATTR_CONTENT_ID); - if (!TextUtils.isEmpty(contentId)) { - String fileName = readText(parser).trim(); - if (!TextUtils.isEmpty(fileName)) { - fileNameToContentIdMap.put(fileName, contentId); - } - } - } else if (TAG_FILE_CONTENT.equals(parser.getName())) { - String contentId = parser.getAttributeValue("", ATTR_CONTENT_ID); - if (!TextUtils.isEmpty(contentId) && - !outContentIdToFileContentMap.containsKey(contentId) && - !contentIdToFileContentMap.containsKey(contentId)) { - String fileContent = readText(parser); - if (!TextUtils.isEmpty(fileContent)) { - contentIdToFileContentMap.put(contentId, fileContent); - } - } - } - } - - state = parser.next(); - } - outFileNameToContentIdMap.putAll(fileNameToContentIdMap); - outContentIdToFileContentMap.putAll(contentIdToFileContentMap); - } - - private static String readText(XmlPullParser parser) - throws IOException, XmlPullParserException { - StringBuffer result = new StringBuffer(); - int state = parser.next(); - while (state == XmlPullParser.TEXT) { - result.append(parser.getText()); - state = parser.next(); - } - return result.toString(); - } - - @VisibleForTesting - static void generateHtml(Map fileNameToContentIdMap, - Map contentIdToFileContentMap, PrintWriter writer) { - List fileNameList = new ArrayList(); - fileNameList.addAll(fileNameToContentIdMap.keySet()); - Collections.sort(fileNameList); - - writer.println(HTML_HEAD_STRING); - - int count = 0; - Map contentIdToOrderMap = new HashMap(); - List contentIdAndFileNamesList = new ArrayList(); - - // Prints all the file list with a link to its license file content. - for (String fileName : fileNameList) { - String contentId = fileNameToContentIdMap.get(fileName); - // Assigns an id to a newly referred license file content. - if (!contentIdToOrderMap.containsKey(contentId)) { - contentIdToOrderMap.put(contentId, count); - - // An index in contentIdAndFileNamesList is the order of each element. - contentIdAndFileNamesList.add(new ContentIdAndFileNames(contentId)); - count++; - } - - int id = contentIdToOrderMap.get(contentId); - contentIdAndFileNamesList.get(id).mFileNameList.add(fileName); - writer.format("
  • %s
  • \n", id, fileName); - } - - writer.println(HTML_MIDDLE_STRING); - - count = 0; - // Prints all contents of the license files in order of id. - for (ContentIdAndFileNames contentIdAndFileNames : contentIdAndFileNamesList) { - writer.format("\n", count); - writer.println("
    Notices for file(s):
    "); - writer.println("
    "); - for (String fileName : contentIdAndFileNames.mFileNameList) { - writer.format("%s
    \n", fileName); - } - writer.println("
    "); - writer.println("
    ");
    -            writer.println(contentIdToFileContentMap.get(
    -                    contentIdAndFileNames.mContentId));
    -            writer.println("
    "); - writer.println(""); - - count++; - } - - writer.println(HTML_REAR_STRING); - } -} diff --git a/src/com/android/settings/LicenseHtmlLoader.java b/src/com/android/settings/LicenseHtmlLoader.java deleted file mode 100644 index 97179269013..00000000000 --- a/src/com/android/settings/LicenseHtmlLoader.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings; - -import android.content.Context; -import android.support.annotation.VisibleForTesting; -import android.util.Log; - -import com.android.settings.utils.AsyncLoader; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -/** - * LicenseHtmlLoader is a loader which loads a license html file from default license xml files. - */ -class LicenseHtmlLoader extends AsyncLoader { - private static final String TAG = "LicenseHtmlLoader"; - - private static final String[] DEFAULT_LICENSE_XML_PATHS = { - "/system/etc/NOTICE.xml.gz", - "/vendor/etc/NOTICE.xml.gz", - "/odm/etc/NOTICE.xml.gz", - "/oem/etc/NOTICE.xml.gz"}; - private static final String NOTICE_HTML_FILE_NAME = "NOTICE.html"; - - private Context mContext; - - public LicenseHtmlLoader(Context context) { - super(context); - mContext = context; - } - - @Override - public File loadInBackground() { - return generateHtmlFromDefaultXmlFiles(); - } - - @Override - protected void onDiscardResult(File f) { - } - - private File generateHtmlFromDefaultXmlFiles() { - final List xmlFiles = getVaildXmlFiles(); - if (xmlFiles.isEmpty()) { - Log.e(TAG, "No notice file exists."); - return null; - } - - File cachedHtmlFile = getCachedHtmlFile(); - if(!isCachedHtmlFileOutdated(xmlFiles, cachedHtmlFile) || - generateHtmlFile(xmlFiles, cachedHtmlFile)) { - return cachedHtmlFile; - } - - return null; - } - - @VisibleForTesting - List getVaildXmlFiles() { - final List xmlFiles = new ArrayList(); - for (final String xmlPath : DEFAULT_LICENSE_XML_PATHS) { - File file = new File(xmlPath); - if (file.exists() && file.length() != 0) { - xmlFiles.add(file); - } - } - return xmlFiles; - } - - @VisibleForTesting - File getCachedHtmlFile() { - return new File(mContext.getCacheDir(), NOTICE_HTML_FILE_NAME); - } - - @VisibleForTesting - boolean isCachedHtmlFileOutdated(List xmlFiles, File cachedHtmlFile) { - boolean outdated = true; - if (cachedHtmlFile.exists() && cachedHtmlFile.length() != 0) { - outdated = false; - for (File file : xmlFiles) { - if (cachedHtmlFile.lastModified() < file.lastModified()) { - outdated = true; - break; - } - } - } - return outdated; - } - - @VisibleForTesting - boolean generateHtmlFile(List xmlFiles, File htmlFile) { - return LicenseHtmlGeneratorFromXml.generateHtml(xmlFiles, htmlFile); - } -} diff --git a/src/com/android/settings/SettingsLicenseActivity.java b/src/com/android/settings/SettingsLicenseActivity.java index 5b23a68990a..ebb1ae13afd 100644 --- a/src/com/android/settings/SettingsLicenseActivity.java +++ b/src/com/android/settings/SettingsLicenseActivity.java @@ -20,12 +20,10 @@ import android.app.Activity; import android.app.LoaderManager; import android.content.ActivityNotFoundException; import android.content.ContentResolver; -import android.content.Context; import android.content.Intent; import android.content.Loader; import android.net.Uri; import android.os.Bundle; -import android.os.StrictMode; import android.os.SystemProperties; import android.support.annotation.VisibleForTesting; import android.support.v4.content.FileProvider; @@ -34,10 +32,9 @@ import android.util.Log; import android.widget.Toast; import com.android.settings.users.RestrictedProfileSettings; +import com.android.settingslib.license.LicenseHtmlLoader; import java.io.File; -import java.util.ArrayList; -import java.util.List; /** * The "dialog" that shows from "License" in the Settings app. @@ -111,9 +108,9 @@ public class SettingsLicenseActivity extends Activity implements return; } showHtmlFromUri(Uri.fromFile(file)); - } + } - private void showHtmlFromUri(Uri uri) { + private void showHtmlFromUri(Uri uri) { // Kick off external viewer due to WebView security restrictions; we // carefully point it at HTMLViewer, since it offers to decompress // before viewing. diff --git a/src/com/android/settings/applications/FetchPackageStorageAsyncLoader.java b/src/com/android/settings/applications/FetchPackageStorageAsyncLoader.java index b39ec3b18b5..9ff96c1f0b6 100644 --- a/src/com/android/settings/applications/FetchPackageStorageAsyncLoader.java +++ b/src/com/android/settings/applications/FetchPackageStorageAsyncLoader.java @@ -24,9 +24,9 @@ import android.os.UserHandle; import android.util.Log; import com.android.internal.util.Preconditions; -import com.android.settings.utils.AsyncLoader; import com.android.settingslib.applications.StorageStatsSource; import com.android.settingslib.applications.StorageStatsSource.AppStorageStats; +import com.android.settingslib.utils.AsyncLoader; import java.io.IOException; diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionLoader.java b/src/com/android/settings/dashboard/suggestions/SuggestionLoader.java index b9d51ce04d6..8c5b46d09fe 100644 --- a/src/com/android/settings/dashboard/suggestions/SuggestionLoader.java +++ b/src/com/android/settings/dashboard/suggestions/SuggestionLoader.java @@ -20,7 +20,7 @@ import android.content.Context; import android.service.settings.suggestions.Suggestion; import android.util.Log; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; import java.util.List; diff --git a/src/com/android/settings/datausage/AppPrefLoader.java b/src/com/android/settings/datausage/AppPrefLoader.java index 30e30eb662f..b6848136868 100644 --- a/src/com/android/settings/datausage/AppPrefLoader.java +++ b/src/com/android/settings/datausage/AppPrefLoader.java @@ -21,7 +21,7 @@ import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.support.v7.preference.Preference; import android.util.ArraySet; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; public class AppPrefLoader extends AsyncLoader> { private ArraySet mPackages; diff --git a/src/com/android/settings/deviceinfo/storage/StorageAsyncLoader.java b/src/com/android/settings/deviceinfo/storage/StorageAsyncLoader.java index 2ce53f65755..0b9b697f473 100644 --- a/src/com/android/settings/deviceinfo/storage/StorageAsyncLoader.java +++ b/src/com/android/settings/deviceinfo/storage/StorageAsyncLoader.java @@ -30,9 +30,9 @@ import android.util.ArraySet; import android.util.Log; import android.util.SparseArray; -import com.android.settings.utils.AsyncLoader; import com.android.settings.wrapper.UserManagerWrapper; import com.android.settingslib.applications.StorageStatsSource; +import com.android.settingslib.utils.AsyncLoader; import com.android.settingslib.wrapper.PackageManagerWrapper; import java.io.IOException; diff --git a/src/com/android/settings/deviceinfo/storage/UserIconLoader.java b/src/com/android/settings/deviceinfo/storage/UserIconLoader.java index 4f00c3c5496..d1c29dfe38a 100644 --- a/src/com/android/settings/deviceinfo/storage/UserIconLoader.java +++ b/src/com/android/settings/deviceinfo/storage/UserIconLoader.java @@ -25,7 +25,7 @@ import android.util.SparseArray; import com.android.internal.util.Preconditions; import com.android.settings.Utils; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; /** * Fetches a user icon as a loader using a given icon loading lambda. diff --git a/src/com/android/settings/deviceinfo/storage/VolumeSizesLoader.java b/src/com/android/settings/deviceinfo/storage/VolumeSizesLoader.java index 720f1512b59..236f55f48b7 100644 --- a/src/com/android/settings/deviceinfo/storage/VolumeSizesLoader.java +++ b/src/com/android/settings/deviceinfo/storage/VolumeSizesLoader.java @@ -21,9 +21,9 @@ import android.content.Context; import android.os.storage.VolumeInfo; import android.support.annotation.VisibleForTesting; -import com.android.settings.utils.AsyncLoader; import com.android.settingslib.deviceinfo.PrivateStorageInfo; import com.android.settingslib.deviceinfo.StorageVolumeProvider; +import com.android.settingslib.utils.AsyncLoader; import java.io.IOException; diff --git a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java index ce22a8cf03f..614eb8006e2 100644 --- a/src/com/android/settings/fuelgauge/BatteryInfoLoader.java +++ b/src/com/android/settings/fuelgauge/BatteryInfoLoader.java @@ -25,7 +25,7 @@ import android.os.BatteryStats; import android.os.SystemClock; import com.android.internal.os.BatteryStatsHelper; import com.android.settings.overlay.FeatureFactory; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; /** * Loader that can be used by classes to load BatteryInfo in a background thread. This loader will diff --git a/src/com/android/settings/fuelgauge/BatteryStatsHelperLoader.java b/src/com/android/settings/fuelgauge/BatteryStatsHelperLoader.java index b81f2823b88..28585ae0b6a 100644 --- a/src/com/android/settings/fuelgauge/BatteryStatsHelperLoader.java +++ b/src/com/android/settings/fuelgauge/BatteryStatsHelperLoader.java @@ -23,7 +23,7 @@ import android.os.UserManager; import android.support.annotation.VisibleForTesting; import com.android.internal.os.BatteryStatsHelper; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; /** * Loader to get new {@link BatteryStatsHelper} in the background diff --git a/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java b/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java index 5f4758a40b7..19aa6390393 100644 --- a/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java +++ b/src/com/android/settings/fuelgauge/DebugEstimatesLoader.java @@ -22,7 +22,7 @@ import android.os.BatteryStats; import android.os.SystemClock; import com.android.internal.os.BatteryStatsHelper; import com.android.settings.overlay.FeatureFactory; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; import java.util.ArrayList; import java.util.List; diff --git a/src/com/android/settings/fuelgauge/PowerUsageBase.java b/src/com/android/settings/fuelgauge/PowerUsageBase.java index 5f46b074b90..b811f208b84 100644 --- a/src/com/android/settings/fuelgauge/PowerUsageBase.java +++ b/src/com/android/settings/fuelgauge/PowerUsageBase.java @@ -26,7 +26,7 @@ import android.view.Menu; import com.android.internal.os.BatteryStatsHelper; import com.android.settings.dashboard.DashboardFragment; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; /** * Common base class for things that need to show the battery usage graph. diff --git a/src/com/android/settings/fuelgauge/anomaly/AnomalyLoader.java b/src/com/android/settings/fuelgauge/anomaly/AnomalyLoader.java index 596eaf5980e..f26b7426e47 100644 --- a/src/com/android/settings/fuelgauge/anomaly/AnomalyLoader.java +++ b/src/com/android/settings/fuelgauge/anomaly/AnomalyLoader.java @@ -26,7 +26,7 @@ import android.util.Log; import com.android.internal.os.BatteryStatsHelper; import com.android.internal.util.ArrayUtils; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; import java.io.FileDescriptor; import java.io.PrintWriter; diff --git a/src/com/android/settings/search/SavedQueryLoader.java b/src/com/android/settings/search/SavedQueryLoader.java index e8efe85f78d..5df3610354f 100644 --- a/src/com/android/settings/search/SavedQueryLoader.java +++ b/src/com/android/settings/search/SavedQueryLoader.java @@ -23,7 +23,7 @@ import android.database.sqlite.SQLiteDatabase; import android.support.annotation.VisibleForTesting; import com.android.settings.search.IndexDatabaseHelper.SavedQueriesColumns; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; import java.util.ArrayList; import java.util.List; diff --git a/src/com/android/settings/search/SavedQueryRecorder.java b/src/com/android/settings/search/SavedQueryRecorder.java index 466af0b4716..b3b0bb80506 100644 --- a/src/com/android/settings/search/SavedQueryRecorder.java +++ b/src/com/android/settings/search/SavedQueryRecorder.java @@ -24,7 +24,7 @@ import android.database.sqlite.SQLiteException; import android.util.Log; import com.android.settings.search.IndexDatabaseHelper; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; import static com.android.settings.search.IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES; diff --git a/src/com/android/settings/search/SavedQueryRemover.java b/src/com/android/settings/search/SavedQueryRemover.java index 77334a5ab55..c6abe978f65 100644 --- a/src/com/android/settings/search/SavedQueryRemover.java +++ b/src/com/android/settings/search/SavedQueryRemover.java @@ -24,7 +24,7 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.util.Log; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; public class SavedQueryRemover extends AsyncLoader { diff --git a/src/com/android/settings/search/SearchResultLoader.java b/src/com/android/settings/search/SearchResultLoader.java index 7ec3146171a..f4abd8e40b9 100644 --- a/src/com/android/settings/search/SearchResultLoader.java +++ b/src/com/android/settings/search/SearchResultLoader.java @@ -1,6 +1,6 @@ package com.android.settings.search; -import com.android.settings.utils.AsyncLoader; +import com.android.settingslib.utils.AsyncLoader; import android.content.Context; diff --git a/src/com/android/settings/utils/AsyncLoader.java b/src/com/android/settings/utils/AsyncLoader.java deleted file mode 100644 index 76c99fa4793..00000000000 --- a/src/com/android/settings/utils/AsyncLoader.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2016 Google Inc. - * Licensed to The Android Open Source Project. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.utils; - -import android.content.AsyncTaskLoader; -import android.content.Context; - -/** - * This class fills in some boilerplate for AsyncTaskLoader to actually load things. - * - * Subclasses need to implement {@link AsyncLoader#loadInBackground()} to perform the actual - * background task, and {@link AsyncLoader#onDiscardResult(T)} to clean up previously loaded - * results. - * - * This loader is based on the MailAsyncTaskLoader from the AOSP EmailUnified repo. - */ -public abstract class AsyncLoader extends AsyncTaskLoader { - private T mResult; - - public AsyncLoader(final Context context) { - super(context); - } - - @Override - protected void onStartLoading() { - if (mResult != null) { - deliverResult(mResult); - } - - if (takeContentChanged() || mResult == null) { - forceLoad(); - } - } - - @Override - protected void onStopLoading() { - cancelLoad(); - } - - @Override - public void deliverResult(final T data) { - if (isReset()) { - if (data != null) { - onDiscardResult(data); - } - return; - } - - final T oldResult = mResult; - mResult = data; - - if (isStarted()) { - super.deliverResult(data); - } - - if (oldResult != null && oldResult != mResult) { - onDiscardResult(oldResult); - } - } - - @Override - protected void onReset() { - super.onReset(); - - onStopLoading(); - - if (mResult != null) { - onDiscardResult(mResult); - } - mResult = null; - } - - @Override - public void onCanceled(final T data) { - super.onCanceled(data); - - if (data != null) { - onDiscardResult(data); - } - } - - /** - * Called when discarding the load results so subclasses can take care of clean-up or - * recycling tasks. This is not called if the same result (by way of pointer equality) is - * returned again by a subsequent call to loadInBackground, or if result is null. - * - * Note that this may be called concurrently with loadInBackground(), and in some circumstances - * may be called more than once for a given object. - * - * @param result The value returned from {@link AsyncLoader#loadInBackground()} which - * is to be discarded. - */ - protected abstract void onDiscardResult(final T result); -} diff --git a/tests/robotests/src/com/android/settings/LicenseHtmlGeneratorFromXmlTest.java b/tests/robotests/src/com/android/settings/LicenseHtmlGeneratorFromXmlTest.java deleted file mode 100644 index ea4b2726aed..00000000000 --- a/tests/robotests/src/com/android/settings/LicenseHtmlGeneratorFromXmlTest.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings; - -import static com.google.common.truth.Truth.assertThat; - -import com.android.settings.testutils.SettingsRobolectricTestRunner; - -import java.util.HashMap; -import java.util.Map; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; -import java.io.StringWriter; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.annotation.Config; -import org.xmlpull.v1.XmlPullParserException; - -@RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) -public class LicenseHtmlGeneratorFromXmlTest { - private static final String VAILD_XML_STRING = - "\n" + - "\n" + - "/file0\n" + - "/file1\n" + - "\n" + - ""; - - private static final String INVAILD_XML_STRING = - "\n" + - "\n" + - "/file0\n" + - "/file1\n" + - "\n" + - ""; - - private static final String EXPECTED_HTML_STRING = - "\n" + - "\n" + - "" + - "\n" + - "
    \n" + - "\n" + - "
    \n" + - "\n" + - "\n" + - "
    \n" + - "
    Notices for file(s):
    \n" + - "
    \n" + - "/file0
    \n" + - "/file1
    \n" + - "
    \n" + - "
    \n" +
    -            "license content #0\n" +
    -            "
    \n" + - "
    \n"; - - @Test - public void testParseValidXmlStream() throws XmlPullParserException, IOException { - Map fileNameToContentIdMap = new HashMap(); - Map contentIdToFileContentMap = new HashMap(); - - LicenseHtmlGeneratorFromXml.parse( - new InputStreamReader(new ByteArrayInputStream(VAILD_XML_STRING.getBytes())), - fileNameToContentIdMap, contentIdToFileContentMap); - assertThat(fileNameToContentIdMap.size()).isEqualTo(2); - assertThat(fileNameToContentIdMap.get("/file0")).isEqualTo("0"); - assertThat(fileNameToContentIdMap.get("/file1")).isEqualTo("0"); - assertThat(contentIdToFileContentMap.size()).isEqualTo(1); - assertThat(contentIdToFileContentMap.get("0")).isEqualTo("license content #0"); - } - - @Test(expected = XmlPullParserException.class) - public void testParseInvalidXmlStream() throws XmlPullParserException, IOException { - Map fileNameToContentIdMap = new HashMap(); - Map contentIdToFileContentMap = new HashMap(); - - LicenseHtmlGeneratorFromXml.parse( - new InputStreamReader(new ByteArrayInputStream(INVAILD_XML_STRING.getBytes())), - fileNameToContentIdMap, contentIdToFileContentMap); - } - - @Test - public void testGenerateHtml() { - Map fileNameToContentIdMap = new HashMap(); - Map contentIdToFileContentMap = new HashMap(); - - fileNameToContentIdMap.put("/file0", "0"); - fileNameToContentIdMap.put("/file1", "0"); - contentIdToFileContentMap.put("0", "license content #0"); - - StringWriter output = new StringWriter(); - LicenseHtmlGeneratorFromXml.generateHtml( - fileNameToContentIdMap, contentIdToFileContentMap, new PrintWriter(output)); - assertThat(output.toString()).isEqualTo(EXPECTED_HTML_STRING); - } -} diff --git a/tests/robotests/src/com/android/settings/LicenseHtmlLoaderTest.java b/tests/robotests/src/com/android/settings/LicenseHtmlLoaderTest.java deleted file mode 100644 index b16d3155532..00000000000 --- a/tests/robotests/src/com/android/settings/LicenseHtmlLoaderTest.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; - -import android.content.Context; - -import com.android.settings.testutils.SettingsRobolectricTestRunner; - -import java.io.File; -import java.util.ArrayList; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.annotation.Config; - -@RunWith(SettingsRobolectricTestRunner.class) -@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) -public class LicenseHtmlLoaderTest { - @Mock - private Context mContext; - - LicenseHtmlLoader newLicenseHtmlLoader(ArrayList xmlFiles, - File cachedHtmlFile, boolean isCachedHtmlFileOutdated, - boolean generateHtmlFileSucceeded) { - LicenseHtmlLoader loader = spy(new LicenseHtmlLoader(mContext)); - doReturn(xmlFiles).when(loader).getVaildXmlFiles(); - doReturn(cachedHtmlFile).when(loader).getCachedHtmlFile(); - doReturn(isCachedHtmlFileOutdated).when(loader).isCachedHtmlFileOutdated(any(), any()); - doReturn(generateHtmlFileSucceeded).when(loader).generateHtmlFile(any(), any()); - return loader; - } - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void testLoadInBackground() { - ArrayList xmlFiles = new ArrayList(); - xmlFiles.add(new File("test.xml")); - File cachedHtmlFile = new File("test.html"); - - LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true); - - assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile); - verify(loader).generateHtmlFile(any(), any()); - } - - @Test - public void testLoadInBackgroundWithNoVaildXmlFiles() { - ArrayList xmlFiles = new ArrayList(); - File cachedHtmlFile = new File("test.html"); - - LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, true); - - assertThat(loader.loadInBackground()).isNull(); - verify(loader, never()).generateHtmlFile(any(), any()); - } - - @Test - public void testLoadInBackgroundWithNonOutdatedCachedHtmlFile() { - ArrayList xmlFiles = new ArrayList(); - xmlFiles.add(new File("test.xml")); - File cachedHtmlFile = new File("test.html"); - - LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, false, true); - - assertThat(loader.loadInBackground()).isEqualTo(cachedHtmlFile); - verify(loader, never()).generateHtmlFile(any(), any()); - } - - @Test - public void testLoadInBackgroundWithGenerateHtmlFileFailed() { - ArrayList xmlFiles = new ArrayList(); - xmlFiles.add(new File("test.xml")); - File cachedHtmlFile = new File("test.html"); - - LicenseHtmlLoader loader = newLicenseHtmlLoader(xmlFiles, cachedHtmlFile, true, false); - - assertThat(loader.loadInBackground()).isNull(); - verify(loader).generateHtmlFile(any(), any()); - } -} diff --git a/tests/robotests/src/com/android/settings/SettingsLicenseActivityTest.java b/tests/robotests/src/com/android/settings/SettingsLicenseActivityTest.java index ecda97edc46..aceb671213e 100644 --- a/tests/robotests/src/com/android/settings/SettingsLicenseActivityTest.java +++ b/tests/robotests/src/com/android/settings/SettingsLicenseActivityTest.java @@ -18,21 +18,19 @@ package com.android.settings; import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyInt; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.robolectric.Shadows.shadowOf; import android.app.Application; -import android.os.SystemProperties; import android.content.Intent; import android.net.Uri; +import android.os.SystemProperties; import com.android.settings.testutils.SettingsRobolectricTestRunner; -import java.io.File; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -42,6 +40,8 @@ import org.robolectric.RuntimeEnvironment; import org.robolectric.android.controller.ActivityController; import org.robolectric.annotation.Config; +import java.io.File; + @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public class SettingsLicenseActivityTest {