diff options
188 files changed, 7543 insertions, 3322 deletions
diff --git a/apct-tests/perftests/multiuser/AndroidManifest.xml b/apct-tests/perftests/multiuser/AndroidManifest.xml index b2a9524d29c4..893c8ca9328b 100644 --- a/apct-tests/perftests/multiuser/AndroidManifest.xml +++ b/apct-tests/perftests/multiuser/AndroidManifest.xml @@ -22,6 +22,7 @@ <uses-permission android:name="android.permission.INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" /> <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" /> + <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" /> <application> <uses-library android:name="android.test.runner" /> diff --git a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java index 32107b4e789e..e74e4a958eb9 100644 --- a/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java +++ b/apct-tests/perftests/multiuser/src/android/multiuser/UserLifecycleTests.java @@ -38,8 +38,10 @@ import android.os.Bundle; import android.os.IBinder; import android.os.IProgressListener; import android.os.RemoteException; +import android.os.SystemProperties; import android.os.UserHandle; import android.os.UserManager; +import android.perftests.utils.ShellHelper; import android.util.Log; import android.view.WindowManagerGlobal; @@ -85,6 +87,14 @@ public class UserLifecycleTests { private static final String DUMMY_PACKAGE_NAME = "perftests.multiuser.apps.dummyapp"; + // Copy of UserSystemPackageInstaller whitelist mode constants. + private static final String PACKAGE_WHITELIST_MODE_PROP = + "persist.debug.user.package_whitelist_mode"; + private static final int USER_TYPE_PACKAGE_WHITELIST_MODE_DISABLE = 0; + private static final int USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE = 0b001; + private static final int USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST = 0b100; + private static final int USER_TYPE_PACKAGE_WHITELIST_MODE_DEVICE_DEFAULT = -1; + private UserManager mUm; private ActivityManager mAm; private IActivityManager mIam; @@ -442,6 +452,55 @@ public class UserLifecycleTests { } } + // TODO: This is just a POC. Do this properly and add more. + /** Tests starting (unlocking) a newly-created profile using the user-type-pkg-whitelist. */ + @Test + public void managedProfileUnlock_usingWhitelist() throws Exception { + assumeTrue(mHasManagedUserFeature); + final int origMode = getUserTypePackageWhitelistMode(); + setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE + | USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST); + + try { + while (mRunner.keepRunning()) { + mRunner.pauseTiming(); + final int userId = createManagedProfile(); + mRunner.resumeTiming(); + + startUserInBackground(userId); + + mRunner.pauseTiming(); + removeUser(userId); + mRunner.resumeTiming(); + } + } finally { + setUserTypePackageWhitelistMode(origMode); + } + } + /** Tests starting (unlocking) a newly-created profile NOT using the user-type-pkg-whitelist. */ + @Test + public void managedProfileUnlock_notUsingWhitelist() throws Exception { + assumeTrue(mHasManagedUserFeature); + final int origMode = getUserTypePackageWhitelistMode(); + setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_DISABLE); + + try { + while (mRunner.keepRunning()) { + mRunner.pauseTiming(); + final int userId = createManagedProfile(); + mRunner.resumeTiming(); + + startUserInBackground(userId); + + mRunner.pauseTiming(); + removeUser(userId); + mRunner.resumeTiming(); + } + } finally { + setUserTypePackageWhitelistMode(origMode); + } + } + /** Creates a new user, returning its userId. */ private int createUserNoFlags() { return createUserWithFlags(/* flags= */ 0); @@ -458,6 +517,10 @@ public class UserLifecycleTests { private int createManagedProfile() { final UserInfo userInfo = mUm.createProfileForUser("TestProfile", UserInfo.FLAG_MANAGED_PROFILE, mAm.getCurrentUser()); + if (userInfo == null) { + throw new IllegalStateException("Creating managed profile failed. Most likely there is " + + "already a pre-existing profile on the device."); + } mUsersToRemove.add(userInfo.id); return userInfo.id; } @@ -627,6 +690,20 @@ public class UserLifecycleTests { } } + /** Gets the PACKAGE_WHITELIST_MODE_PROP System Property. */ + private int getUserTypePackageWhitelistMode() { + return SystemProperties.getInt(PACKAGE_WHITELIST_MODE_PROP, + USER_TYPE_PACKAGE_WHITELIST_MODE_DEVICE_DEFAULT); + } + + /** Sets the PACKAGE_WHITELIST_MODE_PROP System Property to the given value. */ + private void setUserTypePackageWhitelistMode(int mode) { + String result = ShellHelper.runShellCommand( + String.format("setprop %s %d", PACKAGE_WHITELIST_MODE_PROP, mode)); + attestFalse("Failed to set sysprop " + PACKAGE_WHITELIST_MODE_PROP + ": " + result, + result != null && result.contains("Failed")); + } + private void removeUser(int userId) { try { mUm.removeUser(userId); diff --git a/api/current.txt b/api/current.txt index 140b6e030433..fc7685d2da97 100644 --- a/api/current.txt +++ b/api/current.txt @@ -6800,6 +6800,7 @@ package android.app.admin { method public boolean isResetPasswordTokenActive(android.content.ComponentName); method public boolean isSecurityLoggingEnabled(@Nullable android.content.ComponentName); method public boolean isUninstallBlocked(@Nullable android.content.ComponentName, String); + method public boolean isUniqueDeviceAttestationSupported(); method public boolean isUsingUnifiedPassword(@NonNull android.content.ComponentName); method public void lockNow(); method public void lockNow(int); @@ -6981,6 +6982,7 @@ package android.app.admin { field public static final int FLAG_PARENT_CAN_ACCESS_MANAGED = 1; // 0x1 field public static final int ID_TYPE_BASE_INFO = 1; // 0x1 field public static final int ID_TYPE_IMEI = 4; // 0x4 + field public static final int ID_TYPE_INDIVIDUAL_ATTESTATION = 16; // 0x10 field public static final int ID_TYPE_MEID = 8; // 0x8 field public static final int ID_TYPE_SERIAL = 2; // 0x2 field public static final int INSTALLKEY_REQUEST_CREDENTIALS_ACCESS = 1; // 0x1 @@ -23090,8 +23092,9 @@ package android.location { public class LocationManager { method @Deprecated @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean addGpsStatusListener(android.location.GpsStatus.Listener); - method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean addNmeaListener(@NonNull android.location.OnNmeaMessageListener); + method @Deprecated @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean addNmeaListener(@NonNull android.location.OnNmeaMessageListener); method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean addNmeaListener(@NonNull android.location.OnNmeaMessageListener, @Nullable android.os.Handler); + method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean addNmeaListener(@NonNull java.util.concurrent.Executor, @NonNull android.location.OnNmeaMessageListener); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void addProximityAlert(double, double, float, long, @NonNull android.app.PendingIntent); method public void addTestProvider(@NonNull String, boolean, boolean, boolean, boolean, boolean, boolean, boolean, int, int); method @Deprecated public void clearTestProviderEnabled(@NonNull String); @@ -23108,12 +23111,15 @@ package android.location { method @NonNull public java.util.List<java.lang.String> getProviders(@NonNull android.location.Criteria, boolean); method public boolean isLocationEnabled(); method public boolean isProviderEnabled(@NonNull String); - method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssMeasurementsCallback(@NonNull android.location.GnssMeasurementsEvent.Callback); + method @Deprecated @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssMeasurementsCallback(@NonNull android.location.GnssMeasurementsEvent.Callback); method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssMeasurementsCallback(@NonNull android.location.GnssMeasurementsEvent.Callback, @Nullable android.os.Handler); - method public boolean registerGnssNavigationMessageCallback(@NonNull android.location.GnssNavigationMessage.Callback); + method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssMeasurementsCallback(@NonNull java.util.concurrent.Executor, @NonNull android.location.GnssMeasurementsEvent.Callback); + method @Deprecated public boolean registerGnssNavigationMessageCallback(@NonNull android.location.GnssNavigationMessage.Callback); method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssNavigationMessageCallback(@NonNull android.location.GnssNavigationMessage.Callback, @Nullable android.os.Handler); - method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssStatusCallback(@NonNull android.location.GnssStatus.Callback); + method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssNavigationMessageCallback(@NonNull java.util.concurrent.Executor, @NonNull android.location.GnssNavigationMessage.Callback); + method @Deprecated @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssStatusCallback(@NonNull android.location.GnssStatus.Callback); method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssStatusCallback(@NonNull android.location.GnssStatus.Callback, @Nullable android.os.Handler); + method @RequiresPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) public boolean registerGnssStatusCallback(@NonNull java.util.concurrent.Executor, @NonNull android.location.GnssStatus.Callback); method @Deprecated public void removeGpsStatusListener(android.location.GpsStatus.Listener); method public void removeNmeaListener(@NonNull android.location.OnNmeaMessageListener); method @RequiresPermission(anyOf={"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"}, apis="..22") public void removeProximityAlert(@NonNull android.app.PendingIntent); @@ -23122,7 +23128,9 @@ package android.location { method public void removeUpdates(@NonNull android.app.PendingIntent); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull String, long, float, @NonNull android.location.LocationListener); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull String, long, float, @NonNull android.location.LocationListener, @Nullable android.os.Looper); + method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull String, long, float, @NonNull java.util.concurrent.Executor, @NonNull android.location.LocationListener); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(long, float, @NonNull android.location.Criteria, @NonNull android.location.LocationListener, @Nullable android.os.Looper); + method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(long, float, @NonNull android.location.Criteria, @NonNull java.util.concurrent.Executor, @NonNull android.location.LocationListener); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull String, long, float, @NonNull android.app.PendingIntent); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(long, float, @NonNull android.location.Criteria, @NonNull android.app.PendingIntent); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestSingleUpdate(@NonNull String, @NonNull android.location.LocationListener, @Nullable android.os.Looper); diff --git a/api/system-current.txt b/api/system-current.txt index ced3b3c5ba6d..279d2c89808e 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -3432,6 +3432,7 @@ package android.location { method @RequiresPermission(android.Manifest.permission.READ_DEVICE_CONFIG) public boolean isProviderPackage(@NonNull String); method @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) public boolean registerGnssBatchedLocationCallback(long, boolean, @NonNull android.location.BatchedLocationCallback, @Nullable android.os.Handler); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull android.location.LocationRequest, @NonNull android.location.LocationListener, @Nullable android.os.Looper); + method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull android.location.LocationRequest, @NonNull java.util.concurrent.Executor, @NonNull android.location.LocationListener); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull android.location.LocationRequest, @NonNull android.app.PendingIntent); method @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) public void setExtraLocationControllerPackage(@Nullable String); method @RequiresPermission(android.Manifest.permission.LOCATION_HARDWARE) public void setExtraLocationControllerPackageEnabled(boolean); @@ -6180,6 +6181,7 @@ package android.security.keystore { field public static final int ID_TYPE_IMEI = 2; // 0x2 field public static final int ID_TYPE_MEID = 3; // 0x3 field public static final int ID_TYPE_SERIAL = 1; // 0x1 + field public static final int USE_INDIVIDUAL_ATTESTATION = 4; // 0x4 } public class DeviceIdAttestationException extends java.lang.Exception { diff --git a/api/test-current.txt b/api/test-current.txt index 61bdc96d6e29..9b00a42d27a9 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -786,7 +786,7 @@ package android.content.res { public final class AssetManager implements java.lang.AutoCloseable { method @NonNull public String[] getApkPaths(); - method @Nullable public java.util.Map<java.lang.String,java.lang.String> getOverlayableMap(String); + method @Nullable public String getOverlayablesToString(String); } public final class Configuration implements java.lang.Comparable<android.content.res.Configuration> android.os.Parcelable { @@ -1094,6 +1094,7 @@ package android.location { method @NonNull public String[] getIgnoreSettingsWhitelist(); method @NonNull public java.util.List<android.location.LocationRequest> getTestProviderCurrentRequests(String); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull android.location.LocationRequest, @NonNull android.location.LocationListener, @Nullable android.os.Looper); + method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull android.location.LocationRequest, @NonNull java.util.concurrent.Executor, @NonNull android.location.LocationListener); method @RequiresPermission(anyOf={android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}) public void requestLocationUpdates(@NonNull android.location.LocationRequest, @NonNull android.app.PendingIntent); method @RequiresPermission(android.Manifest.permission.WRITE_SECURE_SETTINGS) public void setLocationEnabledForUser(boolean, @NonNull android.os.UserHandle); } @@ -2435,6 +2436,7 @@ package android.security.keystore { field public static final int ID_TYPE_IMEI = 2; // 0x2 field public static final int ID_TYPE_MEID = 3; // 0x3 field public static final int ID_TYPE_SERIAL = 1; // 0x1 + field public static final int USE_INDIVIDUAL_ATTESTATION = 4; // 0x4 } public class DeviceIdAttestationException extends java.lang.Exception { diff --git a/cmds/statsd/Android.bp b/cmds/statsd/Android.bp index 05ff49045b17..c79b0cab35c6 100644 --- a/cmds/statsd/Android.bp +++ b/cmds/statsd/Android.bp @@ -74,6 +74,7 @@ cc_defaults { "src/external/StatsPuller.cpp", "src/external/StatsPullerManager.cpp", "src/external/SubsystemSleepStatePuller.cpp", + "src/external/SurfaceflingerStatsPuller.cpp", "src/external/TrainInfoPuller.cpp", "src/FieldValue.cpp", "src/guardrail/StatsdStats.cpp", @@ -138,6 +139,7 @@ cc_defaults { "libservices", "libstatslog", "libsysutils", + "libtimestats_proto", "libutils", ], } @@ -239,6 +241,7 @@ cc_test { "tests/external/IncidentReportArgs_test.cpp", "tests/external/puller_util_test.cpp", "tests/external/StatsPuller_test.cpp", + "tests/external/SurfaceflingerStatsPuller_test.cpp", "tests/FieldValue_test.cpp", "tests/guardrail/StatsdStats_test.cpp", "tests/indexed_priority_queue_test.cpp", diff --git a/cmds/statsd/src/external/StatsPullerManager.cpp b/cmds/statsd/src/external/StatsPullerManager.cpp index f69e2d09ad23..7a183a3b1b2e 100644 --- a/cmds/statsd/src/external/StatsPullerManager.cpp +++ b/cmds/statsd/src/external/StatsPullerManager.cpp @@ -17,12 +17,17 @@ #define DEBUG false #include "Log.h" +#include "StatsPullerManager.h" + #include <android/os/IStatsCompanionService.h> #include <android/os/IStatsPullerCallback.h> #include <cutils/log.h> #include <math.h> #include <stdint.h> + #include <algorithm> +#include <iostream> + #include "../StatsService.h" #include "../logd/LogEvent.h" #include "../stats_log_util.h" @@ -32,13 +37,11 @@ #include "ResourceHealthManagerPuller.h" #include "StatsCallbackPuller.h" #include "StatsCompanionServicePuller.h" -#include "StatsPullerManager.h" #include "SubsystemSleepStatePuller.h" +#include "SurfaceflingerStatsPuller.h" #include "TrainInfoPuller.h" #include "statslog.h" -#include <iostream> - using std::make_shared; using std::map; using std::shared_ptr; @@ -269,6 +272,10 @@ std::map<int, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = { // App ops {android::util::APP_OPS, {.puller = new StatsCompanionServicePuller(android::util::APP_OPS)}}, + // SurfaceflingerStatsGlobalInfo + {android::util::SURFACEFLINGER_STATS_GLOBAL_INFO, + {.puller = + new SurfaceflingerStatsPuller(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO)}}, }; StatsPullerManager::StatsPullerManager() : mNextPullTimeNs(NO_ALARM_UPDATE) { diff --git a/cmds/statsd/src/external/SurfaceflingerStatsPuller.cpp b/cmds/statsd/src/external/SurfaceflingerStatsPuller.cpp new file mode 100644 index 000000000000..23b2236f35f2 --- /dev/null +++ b/cmds/statsd/src/external/SurfaceflingerStatsPuller.cpp @@ -0,0 +1,99 @@ +/* + * Copyright 2019 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. + */ + +#include "SurfaceflingerStatsPuller.h" + +#include <cutils/compiler.h> + +#include <numeric> + +#include "logd/LogEvent.h" +#include "stats_log_util.h" +#include "statslog.h" + +namespace android { +namespace os { +namespace statsd { + +SurfaceflingerStatsPuller::SurfaceflingerStatsPuller(const int tagId) : StatsPuller(tagId) { +} + +bool SurfaceflingerStatsPuller::PullInternal(std::vector<std::shared_ptr<LogEvent>>* data) { + switch (mTagId) { + case android::util::SURFACEFLINGER_STATS_GLOBAL_INFO: + return pullGlobalInfo(data); + default: + break; + } + + return false; +} + +static int64_t getTotalTime( + const google::protobuf::RepeatedPtrField<surfaceflinger::SFTimeStatsHistogramBucketProto>& + buckets) { + int64_t total = 0; + for (const auto& bucket : buckets) { + if (bucket.time_millis() == 1000) { + continue; + } + + total += bucket.time_millis() * bucket.frame_count(); + } + + return total; +} + +bool SurfaceflingerStatsPuller::pullGlobalInfo(std::vector<std::shared_ptr<LogEvent>>* data) { + std::string protoBytes; + if (CC_UNLIKELY(mStatsProvider)) { + protoBytes = mStatsProvider(); + } else { + std::unique_ptr<FILE, decltype(&pclose)> pipe(popen("dumpsys SurfaceFlinger --timestats -dump --proto", "r"), pclose); + if (!pipe.get()) { + return false; + } + char buf[1024]; + size_t bytesRead = 0; + do { + bytesRead = fread(buf, 1, sizeof(buf), pipe.get()); + protoBytes.append(buf, bytesRead); + } while (bytesRead > 0); + } + surfaceflinger::SFTimeStatsGlobalProto proto; + proto.ParseFromString(protoBytes); + + int64_t totalTime = getTotalTime(proto.present_to_present()); + + data->clear(); + data->reserve(1); + std::shared_ptr<LogEvent> event = + make_shared<LogEvent>(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO, getWallClockNs(), + getElapsedRealtimeNs()); + if (!event->write(proto.total_frames())) return false; + if (!event->write(proto.missed_frames())) return false; + if (!event->write(proto.client_composition_frames())) return false; + if (!event->write(proto.display_on_time())) return false; + if (!event->write(totalTime)) return false; + event->init(); + data->emplace_back(event); + + return true; +} + +} // namespace statsd +} // namespace os +} // namespace android diff --git a/cmds/statsd/src/external/SurfaceflingerStatsPuller.h b/cmds/statsd/src/external/SurfaceflingerStatsPuller.h new file mode 100644 index 000000000000..ed7153edf797 --- /dev/null +++ b/cmds/statsd/src/external/SurfaceflingerStatsPuller.h @@ -0,0 +1,48 @@ +/* + * Copyright 2019 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. + */ + +#pragma once + +#include <timestatsproto/TimeStatsProtoHeader.h> + +#include "StatsPuller.h" + +namespace android { +namespace os { +namespace statsd { + +/** + * Pull metrics from Surfaceflinger + */ +class SurfaceflingerStatsPuller : public StatsPuller { +public: + explicit SurfaceflingerStatsPuller(const int tagId); + + // StatsPuller interface + bool PullInternal(std::vector<std::shared_ptr<LogEvent>>* data) override; + +protected: + // Test-only, for injecting fake data + using StatsProvider = std::function<std::string()>; + StatsProvider mStatsProvider; + +private: + bool pullGlobalInfo(std::vector<std::shared_ptr<LogEvent>>* data); +}; + +} // namespace statsd +} // namespace os +} // namespace android diff --git a/cmds/statsd/src/stats_log.proto b/cmds/statsd/src/stats_log.proto index d9c04f248af0..e45e24fe49d4 100644 --- a/cmds/statsd/src/stats_log.proto +++ b/cmds/statsd/src/stats_log.proto @@ -41,6 +41,15 @@ message DimensionsValueTuple { repeated DimensionsValue dimensions_value = 1; } +message StateValue { + optional int32 atom_id = 1; + + oneof contents { + int64 group_id = 2; + int32 value = 3; + } +} + message EventMetricData { optional int64 elapsed_timestamp_nanos = 1; @@ -66,12 +75,14 @@ message CountBucketInfo { message CountMetricData { optional DimensionsValue dimensions_in_what = 1; - optional DimensionsValue dimensions_in_condition = 2 [deprecated = true]; + repeated StateValue slice_by_state = 6; repeated CountBucketInfo bucket_info = 3; repeated DimensionsValue dimension_leaf_values_in_what = 4; + optional DimensionsValue dimensions_in_condition = 2 [deprecated = true]; + repeated DimensionsValue dimension_leaf_values_in_condition = 5 [deprecated = true]; } diff --git a/cmds/statsd/src/statsd_config.proto b/cmds/statsd/src/statsd_config.proto index 1b7f39806608..c107397b0273 100644 --- a/cmds/statsd/src/statsd_config.proto +++ b/cmds/statsd/src/statsd_config.proto @@ -150,6 +150,24 @@ message Predicate { } } +message StateMap { + message StateGroup { + optional int64 group_id = 1; + + repeated int32 value = 2; + } + + repeated StateGroup group = 1; +} + +message State { + optional int64 id = 1; + + optional int32 atom_id = 2; + + optional StateMap map = 3; +} + message MetricConditionLink { optional int64 condition = 1; @@ -158,6 +176,14 @@ message MetricConditionLink { optional FieldMatcher fields_in_condition = 3; } +message MetricStateLink { + optional int64 state = 1; + + optional FieldMatcher fields_in_what = 2; + + optional FieldMatcher fields_in_state = 3; +} + message FieldFilter { optional bool include_all = 1 [default = false]; optional FieldMatcher fields = 2; @@ -182,11 +208,15 @@ message CountMetric { optional FieldMatcher dimensions_in_what = 4; - optional FieldMatcher dimensions_in_condition = 7 [deprecated = true]; + repeated int64 slice_by_state = 8; optional TimeUnit bucket = 5; repeated MetricConditionLink links = 6; + + repeated MetricStateLink state_link = 9; + + optional FieldMatcher dimensions_in_condition = 7 [deprecated = true]; } message DurationMetric { @@ -438,6 +468,8 @@ message StatsdConfig { optional bool persist_locally = 20 [default = false]; + repeated State state = 21; + // Field number 1000 is reserved for later use. reserved 1000; } diff --git a/cmds/statsd/tests/StatsLogProcessor_test.cpp b/cmds/statsd/tests/StatsLogProcessor_test.cpp index fe25a257aa67..76ee9a6e5996 100644 --- a/cmds/statsd/tests/StatsLogProcessor_test.cpp +++ b/cmds/statsd/tests/StatsLogProcessor_test.cpp @@ -1713,6 +1713,11 @@ TEST(StatsLogProcessorTest, TestActivationsPersistAcrossSystemServerRestart) { EXPECT_EQ(kActive, activation1004->state); EXPECT_EQ(ACTIVATE_IMMEDIATELY, activation1004->activationType); // }}}------------------------------------------------------------------------------ + + // Clear the data stored on disk as a result of the system server death. + vector<uint8_t> buffer; + processor->onDumpReport(cfgKey1, configAddedTimeNs + NS_PER_SEC, false, true, + ADB_DUMP, FAST, &buffer); } #else diff --git a/cmds/statsd/tests/e2e/ConfigTtl_e2e_test.cpp b/cmds/statsd/tests/e2e/ConfigTtl_e2e_test.cpp index b98dc60086ac..325e869e5a9b 100644 --- a/cmds/statsd/tests/e2e/ConfigTtl_e2e_test.cpp +++ b/cmds/statsd/tests/e2e/ConfigTtl_e2e_test.cpp @@ -96,6 +96,11 @@ TEST(ConfigTtlE2eTest, TestCountMetric) { EXPECT_EQ((int64_t)(bucketStartTimeNs + 25 * bucketSizeNs + 2 + 2 * 3600 * NS_PER_SEC), processor->mMetricsManagers.begin()->second->getTtlEndNs()); + + // Clear the data stored on disk as a result of the ttl. + vector<uint8_t> buffer; + processor->onDumpReport(cfgKey, bucketStartTimeNs + 25 * bucketSizeNs + 3, false, true, + ADB_DUMP, FAST, &buffer); } diff --git a/cmds/statsd/tests/external/SurfaceflingerStatsPuller_test.cpp b/cmds/statsd/tests/external/SurfaceflingerStatsPuller_test.cpp new file mode 100644 index 000000000000..5c9636fa99cc --- /dev/null +++ b/cmds/statsd/tests/external/SurfaceflingerStatsPuller_test.cpp @@ -0,0 +1,95 @@ +/* + * Copyright 2019 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. + */ + +#undef LOG_TAG +#define LOG_TAG "SurfaceflingerStatsPuller_test" + +#include "src/external/SurfaceflingerStatsPuller.h" + +#include <gtest/gtest.h> +#include <log/log.h> + +#ifdef __ANDROID__ + +namespace android { +namespace os { +namespace statsd { + +class TestableSurfaceflingerStatsPuller : public SurfaceflingerStatsPuller { +public: + TestableSurfaceflingerStatsPuller(const int tagId) : SurfaceflingerStatsPuller(tagId){}; + + void injectStats(const StatsProvider& statsProvider) { + mStatsProvider = statsProvider; + } +}; + +class SurfaceflingerStatsPullerTest : public ::testing::Test { +public: + SurfaceflingerStatsPullerTest() { + const ::testing::TestInfo* const test_info = + ::testing::UnitTest::GetInstance()->current_test_info(); + ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); + } + + ~SurfaceflingerStatsPullerTest() { + const ::testing::TestInfo* const test_info = + ::testing::UnitTest::GetInstance()->current_test_info(); + ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); + } +}; + +TEST_F(SurfaceflingerStatsPullerTest, pullGlobalStats) { + surfaceflinger::SFTimeStatsGlobalProto proto; + proto.set_total_frames(1); + proto.set_missed_frames(2); + proto.set_client_composition_frames(2); + proto.set_display_on_time(4); + + auto bucketOne = proto.add_present_to_present(); + bucketOne->set_time_millis(2); + bucketOne->set_frame_count(4); + auto bucketTwo = proto.add_present_to_present(); + bucketTwo->set_time_millis(4); + bucketTwo->set_frame_count(1); + auto bucketThree = proto.add_present_to_present(); + bucketThree->set_time_millis(1000); + bucketThree->set_frame_count(1); + static constexpr int64_t expectedAnimationMillis = 12; + TestableSurfaceflingerStatsPuller puller(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO); + + puller.injectStats([&] { + return proto.SerializeAsString(); + }); + puller.ForceClearCache(); + vector<std::shared_ptr<LogEvent>> outData; + puller.Pull(&outData); + + ASSERT_EQ(1, outData.size()); + EXPECT_EQ(android::util::SURFACEFLINGER_STATS_GLOBAL_INFO, outData[0]->GetTagId()); + EXPECT_EQ(proto.total_frames(), outData[0]->getValues()[0].mValue.long_value); + EXPECT_EQ(proto.missed_frames(), outData[0]->getValues()[1].mValue.long_value); + EXPECT_EQ(proto.client_composition_frames(), outData[0]->getValues()[2].mValue.long_value); + EXPECT_EQ(proto.display_on_time(), outData[0]->getValues()[3].mValue.long_value); + EXPECT_EQ(expectedAnimationMillis, outData[0]->getValues()[4].mValue.long_value); +} + +} // namespace statsd +} // namespace os +} // namespace android +#else +GTEST_LOG_(INFO) << "This test does nothing.\n"; +#endif diff --git a/config/hiddenapi-greylist.txt b/config/hiddenapi-greylist.txt index a6e1f0ab0cde..e1cf7c1a583b 100644 --- a/config/hiddenapi-greylist.txt +++ b/config/hiddenapi-greylist.txt @@ -218,7 +218,6 @@ Landroid/location/ILocationManager$Stub$Proxy;-><init>(Landroid/os/IBinder;)V Landroid/location/ILocationManager$Stub;->asInterface(Landroid/os/IBinder;)Landroid/location/ILocationManager; Landroid/location/ILocationManager$Stub;->TRANSACTION_getAllProviders:I Landroid/location/INetInitiatedListener$Stub;-><init>()V -Landroid/location/LocationManager$ListenerTransport;-><init>(Landroid/location/LocationManager;Landroid/location/LocationListener;Landroid/os/Looper;)V Landroid/Manifest$permission;->CAPTURE_SECURE_VIDEO_OUTPUT:Ljava/lang/String; Landroid/Manifest$permission;->CAPTURE_VIDEO_OUTPUT:Ljava/lang/String; Landroid/Manifest$permission;->READ_FRAME_BUFFER:Ljava/lang/String; @@ -1150,6 +1149,8 @@ Lcom/android/internal/statusbar/IStatusBar$Stub;->asInterface(Landroid/os/IBinde Lcom/android/internal/statusbar/IStatusBarService$Stub;-><init>()V Lcom/android/internal/statusbar/IStatusBarService$Stub;->asInterface(Landroid/os/IBinder;)Lcom/android/internal/statusbar/IStatusBarService; Lcom/android/internal/telecom/ITelecomService$Stub;->asInterface(Landroid/os/IBinder;)Lcom/android/internal/telecom/ITelecomService; +Lcom/android/internal/telephony/IIccPhoneBook$Stub$Proxy;->mRemote:Landroid/os/IBinder; +Lcom/android/internal/telephony/IIccPhoneBook$Stub;->asInterface(Landroid/os/IBinder;)Lcom/android/internal/telephony/IIccPhoneBook; Lcom/android/internal/telephony/IMms$Stub;->asInterface(Landroid/os/IBinder;)Lcom/android/internal/telephony/IMms; Lcom/android/internal/telephony/IPhoneStateListener$Stub;->asInterface(Landroid/os/IBinder;)Lcom/android/internal/telephony/IPhoneStateListener; Lcom/android/internal/telephony/IPhoneSubInfo$Stub$Proxy;-><init>(Landroid/os/IBinder;)V @@ -1157,6 +1158,7 @@ Lcom/android/internal/telephony/IPhoneSubInfo$Stub;->asInterface(Landroid/os/IBi Lcom/android/internal/telephony/IPhoneSubInfo$Stub;->TRANSACTION_getDeviceId:I Lcom/android/internal/telephony/ISms$Stub;-><init>()V Lcom/android/internal/telephony/ISms$Stub;->asInterface(Landroid/os/IBinder;)Lcom/android/internal/telephony/ISms; +Lcom/android/internal/telephony/ISub$Stub$Proxy;-><init>(Landroid/os/IBinder;)V Lcom/android/internal/telephony/ISub$Stub;->asInterface(Landroid/os/IBinder;)Lcom/android/internal/telephony/ISub; Lcom/android/internal/telephony/ITelephony$Stub$Proxy;-><init>(Landroid/os/IBinder;)V Lcom/android/internal/telephony/ITelephony$Stub$Proxy;->mRemote:Landroid/os/IBinder; @@ -1459,4 +1461,5 @@ Lcom/google/android/mms/util/SqliteWrapper;->insert(Landroid/content/Context;Lan Lcom/google/android/mms/util/SqliteWrapper;->query(Landroid/content/Context;Landroid/content/ContentResolver;Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor; Lcom/google/android/mms/util/SqliteWrapper;->requery(Landroid/content/Context;Landroid/database/Cursor;)Z Lcom/google/android/mms/util/SqliteWrapper;->update(Landroid/content/Context;Landroid/content/ContentResolver;Landroid/net/Uri;Landroid/content/ContentValues;Ljava/lang/String;[Ljava/lang/String;)I +Lcom/google/android/mms/pdu/PduParser;->$assertionsDisabled:Z Lcom/google/android/util/AbstractMessageParser$Token$Type;->values()[Lcom/google/android/util/AbstractMessageParser$Token$Type; diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index 2847f77de73d..cf36032e8a05 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -2477,17 +2477,13 @@ public class Activity extends ContextThemeWrapper getAutofillManager().onInvisibleForAutofill(); } - if (isFinishing()) { - if (mAutoFillResetNeeded) { - getAutofillManager().onActivityFinishing(); - } else if (mIntent != null - && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) { - // Activity was launched when user tapped a link in the Autofill Save UI - since - // user launched another activity, the Save UI should not be restored when this - // activity is finished. - getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL, - mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)); - } + if (isFinishing() && !mAutoFillResetNeeded && mIntent != null + && mIntent.hasExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)) { + // Activity was launched when user tapped a link in the Autofill Save UI - since + // user launched another activity, the Save UI should not be restored when this + // activity is finished. + getAutofillManager().onPendingSaveUi(AutofillManager.PENDING_UI_OPERATION_CANCEL, + mIntent.getIBinderExtra(AutofillManager.EXTRA_RESTORE_SESSION_TOKEN)); } mEnterAnimationComplete = false; } @@ -2525,6 +2521,10 @@ public class Activity extends ContextThemeWrapper if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this); mCalled = true; + if (isFinishing() && mAutoFillResetNeeded) { + getAutofillManager().onActivityFinishing(); + } + // dismiss any dialogs we are managing. if (mManagedDialogs != null) { final int numDialogs = mManagedDialogs.size(); diff --git a/core/java/android/app/AppCompatCallbacks.java b/core/java/android/app/AppCompatCallbacks.java index 08c97eb284e3..19d158dedd06 100644 --- a/core/java/android/app/AppCompatCallbacks.java +++ b/core/java/android/app/AppCompatCallbacks.java @@ -18,7 +18,6 @@ package android.app; import android.compat.Compatibility; import android.os.Process; -import android.util.Log; import android.util.StatsLog; import com.android.internal.compat.ChangeReporter; @@ -31,8 +30,6 @@ import java.util.Arrays; * @hide */ public final class AppCompatCallbacks extends Compatibility.Callbacks { - private static final String TAG = "Compatibility"; - private final long[] mDisabledChanges; private final ChangeReporter mChangeReporter; @@ -48,7 +45,8 @@ public final class AppCompatCallbacks extends Compatibility.Callbacks { private AppCompatCallbacks(long[] disabledChanges) { mDisabledChanges = Arrays.copyOf(disabledChanges, disabledChanges.length); Arrays.sort(mDisabledChanges); - mChangeReporter = new ChangeReporter(); + mChangeReporter = new ChangeReporter( + StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__APP_PROCESS); } protected void reportChange(long changeId) { @@ -67,10 +65,7 @@ public final class AppCompatCallbacks extends Compatibility.Callbacks { private void reportChange(long changeId, int state) { int uid = Process.myUid(); - //TODO(b/138374585): Implement rate limiting for the logs. - Log.d(TAG, ChangeReporter.createLogString(uid, changeId, state)); - mChangeReporter.reportChange(uid, changeId, - state, /* source */StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__APP_PROCESS); + mChangeReporter.reportChange(uid, changeId, state); } } diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java index 2f03ed484e96..efb9f6bb88f1 100644 --- a/core/java/android/app/Notification.java +++ b/core/java/android/app/Notification.java @@ -10511,12 +10511,7 @@ public class Notification implements Parcelable final StandardTemplateParams fillTextsFrom(Builder b) { Bundle extras = b.mN.extras; this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE)); - - CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT); - if (TextUtils.isEmpty(text)) { - text = extras.getCharSequence(EXTRA_TEXT); - } - this.text = b.processLegacyText(text); + this.text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT)); this.summaryText = extras.getCharSequence(EXTRA_SUB_TEXT); return this; } diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java index 64ddfc106dcf..c3c383ce5e55 100644 --- a/core/java/android/app/admin/DevicePolicyManager.java +++ b/core/java/android/app/admin/DevicePolicyManager.java @@ -2079,7 +2079,8 @@ public class DevicePolicyManager { ID_TYPE_BASE_INFO, ID_TYPE_SERIAL, ID_TYPE_IMEI, - ID_TYPE_MEID + ID_TYPE_MEID, + ID_TYPE_INDIVIDUAL_ATTESTATION }) public @interface AttestationIdType {} @@ -2114,6 +2115,14 @@ public class DevicePolicyManager { public static final int ID_TYPE_MEID = 8; /** + * Specifies that the device should attest using an individual attestation certificate. + * For use with {@link #generateKeyPair}. + * + * @see #generateKeyPair + */ + public static final int ID_TYPE_INDIVIDUAL_ATTESTATION = 16; + + /** * Service-specific error code for {@link #generateKeyPair}: * Indicates the call has failed due to StrongBox unavailability. * @hide @@ -2669,7 +2678,10 @@ public class DevicePolicyManager { * only imposed if the administrator has also requested either {@link #PASSWORD_QUALITY_NUMERIC} * , {@link #PASSWORD_QUALITY_NUMERIC_COMPLEX}, {@link #PASSWORD_QUALITY_ALPHABETIC}, * {@link #PASSWORD_QUALITY_ALPHANUMERIC}, or {@link #PASSWORD_QUALITY_COMPLEX} with - * {@link #setPasswordQuality}. + * {@link #setPasswordQuality}. If an app targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above enforces this constraint without settings + * password quality to one of these values first, this method will throw + * {@link IllegalStateException}. * <p> * On devices not supporting {@link PackageManager#FEATURE_SECURE_LOCK_SCREEN} feature, the * password is always treated as empty. @@ -2684,9 +2696,12 @@ public class DevicePolicyManager { * * @param admin Which {@link DeviceAdminReceiver} this request is associated with. * @param length The new desired minimum password length. A value of 0 means there is no - * restriction. + * restriction. * @throws SecurityException if {@code admin} is not an active administrator or {@code admin} - * does not use {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} + * does not use {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} + * @throws IllegalStateException if the calling app is targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above and didn't set a sufficient password + * quality requirement prior to calling this method. */ public void setPasswordMinimumLength(@NonNull ComponentName admin, int length) { if (mService != null) { @@ -2738,7 +2753,10 @@ public class DevicePolicyManager { * place immediately. To prompt the user for a new password, use * {@link #ACTION_SET_NEW_PASSWORD} or {@link #ACTION_SET_NEW_PARENT_PROFILE_PASSWORD} after * setting this value. This constraint is only imposed if the administrator has also requested - * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The default value is 0. + * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. If an app targeting + * SDK level {@link android.os.Build.VERSION_CODES#R} and above enforces this constraint without + * settings password quality to {@link #PASSWORD_QUALITY_COMPLEX} first, this method will throw + * {@link IllegalStateException}. The default value is 0. * <p> * On devices not supporting {@link PackageManager#FEATURE_SECURE_LOCK_SCREEN} feature, the * password is always treated as empty. @@ -2756,6 +2774,9 @@ public class DevicePolicyManager { * A value of 0 means there is no restriction. * @throws SecurityException if {@code admin} is not an active administrator or {@code admin} * does not use {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} + * @throws IllegalStateException if the calling app is targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above and didn't set a sufficient password + * quality requirement prior to calling this method. */ public void setPasswordMinimumUpperCase(@NonNull ComponentName admin, int length) { if (mService != null) { @@ -2814,7 +2835,10 @@ public class DevicePolicyManager { * place immediately. To prompt the user for a new password, use * {@link #ACTION_SET_NEW_PASSWORD} or {@link #ACTION_SET_NEW_PARENT_PROFILE_PASSWORD} after * setting this value. This constraint is only imposed if the administrator has also requested - * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The default value is 0. + * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. If an app targeting + * SDK level {@link android.os.Build.VERSION_CODES#R} and above enforces this constraint without + * settings password quality to {@link #PASSWORD_QUALITY_COMPLEX} first, this method will throw + * {@link IllegalStateException}. The default value is 0. * <p> * On devices not supporting {@link PackageManager#FEATURE_SECURE_LOCK_SCREEN} feature, the * password is always treated as empty. @@ -2832,6 +2856,9 @@ public class DevicePolicyManager { * A value of 0 means there is no restriction. * @throws SecurityException if {@code admin} is not an active administrator or {@code admin} * does not use {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} + * @throws IllegalStateException if the calling app is targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above and didn't set a sufficient password + * quality requirement prior to calling this method. */ public void setPasswordMinimumLowerCase(@NonNull ComponentName admin, int length) { if (mService != null) { @@ -2890,7 +2917,10 @@ public class DevicePolicyManager { * immediately. To prompt the user for a new password, use {@link #ACTION_SET_NEW_PASSWORD} or * {@link #ACTION_SET_NEW_PARENT_PROFILE_PASSWORD} after setting this value. This constraint is * only imposed if the administrator has also requested {@link #PASSWORD_QUALITY_COMPLEX} with - * {@link #setPasswordQuality}. The default value is 1. + * {@link #setPasswordQuality}. If an app targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above enforces this constraint without settings + * password quality to {@link #PASSWORD_QUALITY_COMPLEX} first, this method will throw + * {@link IllegalStateException}. The default value is 1. * <p> * On devices not supporting {@link PackageManager#FEATURE_SECURE_LOCK_SCREEN} feature, the * password is always treated as empty. @@ -2908,6 +2938,9 @@ public class DevicePolicyManager { * 0 means there is no restriction. * @throws SecurityException if {@code admin} is not an active administrator or {@code admin} * does not use {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} + * @throws IllegalStateException if the calling app is targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above and didn't set a sufficient password + * quality requirement prior to calling this method. */ public void setPasswordMinimumLetters(@NonNull ComponentName admin, int length) { if (mService != null) { @@ -2965,7 +2998,10 @@ public class DevicePolicyManager { * place immediately. To prompt the user for a new password, use * {@link #ACTION_SET_NEW_PASSWORD} or {@link #ACTION_SET_NEW_PARENT_PROFILE_PASSWORD} after * setting this value. This constraint is only imposed if the administrator has also requested - * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The default value is 1. + * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. If an app targeting + * SDK level {@link android.os.Build.VERSION_CODES#R} and above enforces this constraint without + * settings password quality to {@link #PASSWORD_QUALITY_COMPLEX} first, this method will throw + * {@link IllegalStateException}. The default value is 1. * <p> * On devices not supporting {@link PackageManager#FEATURE_SECURE_LOCK_SCREEN} feature, the * password is always treated as empty. @@ -2983,6 +3019,9 @@ public class DevicePolicyManager { * value of 0 means there is no restriction. * @throws SecurityException if {@code admin} is not an active administrator or {@code admin} * does not use {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} + * @throws IllegalStateException if the calling app is targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above and didn't set a sufficient password + * quality requirement prior to calling this method. */ public void setPasswordMinimumNumeric(@NonNull ComponentName admin, int length) { if (mService != null) { @@ -3040,7 +3079,10 @@ public class DevicePolicyManager { * immediately. To prompt the user for a new password, use {@link #ACTION_SET_NEW_PASSWORD} or * {@link #ACTION_SET_NEW_PARENT_PROFILE_PASSWORD} after setting this value. This constraint is * only imposed if the administrator has also requested {@link #PASSWORD_QUALITY_COMPLEX} with - * {@link #setPasswordQuality}. The default value is 1. + * {@link #setPasswordQuality}. If an app targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above enforces this constraint without settings + * password quality to {@link #PASSWORD_QUALITY_COMPLEX} first, this method will throw + * {@link IllegalStateException}. The default value is 1. * <p> * On devices not supporting {@link PackageManager#FEATURE_SECURE_LOCK_SCREEN} feature, the * password is always treated as empty. @@ -3058,6 +3100,9 @@ public class DevicePolicyManager { * 0 means there is no restriction. * @throws SecurityException if {@code admin} is not an active administrator or {@code admin} * does not use {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} + * @throws IllegalStateException if the calling app is targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above and didn't set a sufficient password + * quality requirement prior to calling this method. */ public void setPasswordMinimumSymbols(@NonNull ComponentName admin, int length) { if (mService != null) { @@ -3114,7 +3159,10 @@ public class DevicePolicyManager { * one, so the change does not take place immediately. To prompt the user for a new password, * use {@link #ACTION_SET_NEW_PASSWORD} or {@link #ACTION_SET_NEW_PARENT_PROFILE_PASSWORD} after * setting this value. This constraint is only imposed if the administrator has also requested - * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The default value is 0. + * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. If an app targeting + * SDK level {@link android.os.Build.VERSION_CODES#R} and above enforces this constraint without + * settings password quality to {@link #PASSWORD_QUALITY_COMPLEX} first, this method will throw + * {@link IllegalStateException}. The default value is 0. * <p> * On devices not supporting {@link PackageManager#FEATURE_SECURE_LOCK_SCREEN} feature, the * password is always treated as empty. @@ -3132,6 +3180,9 @@ public class DevicePolicyManager { * 0 means there is no restriction. * @throws SecurityException if {@code admin} is not an active administrator or {@code admin} * does not use {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} + * @throws IllegalStateException if the calling app is targeting SDK level + * {@link android.os.Build.VERSION_CODES#R} and above and didn't set a sufficient password + * quality requirement prior to calling this method. */ public void setPasswordMinimumNonLetter(@NonNull ComponentName admin, int length) { if (mService != null) { @@ -4892,24 +4943,47 @@ public class DevicePolicyManager { * have been given to access the key and certificates associated with this alias will be * revoked. * + * <p>Attestation: to enable attestation, set an attestation challenge in {@code keySpec} via + * {@link KeyGenParameterSpec.Builder#setAttestationChallenge}. By specifying flags to the + * {@code idAttestationFlags} parameter, it is possible to request the device's unique + * identity to be included in the attestation record. + * + * <p>Specific identifiers can be included in the attestation record, and an individual + * attestation certificate can be used to sign the attestation record. To find out if the device + * supports these features, refer to {@link #isDeviceIdAttestationSupported()} and + * {@link #isUniqueDeviceAttestationSupported()}. + * + * <p>Device owner, profile owner and their delegated certificate installer can use + * {@link #ID_TYPE_BASE_INFO} to request inclusion of the general device information + * including manufacturer, model, brand, device and product in the attestation record. + * Only device owner and their delegated certificate installer can use + * {@link #ID_TYPE_SERIAL}, {@link #ID_TYPE_IMEI} and {@link #ID_TYPE_MEID} to request + * unique device identifiers to be attested (the serial number, IMEI and MEID correspondingly), + * if supported by the device (see {@link #isDeviceIdAttestationSupported()}). + * Additionally, device owner and their delegated certificate installer can also request the + * attestation record to be signed using an individual attestation certificate by specifying + * the {@link #ID_TYPE_INDIVIDUAL_ATTESTATION} flag (if supported by the device, see + * {@link #isUniqueDeviceAttestationSupported()}). + * <p> + * If any of {@link #ID_TYPE_SERIAL}, {@link #ID_TYPE_IMEI} and {@link #ID_TYPE_MEID} + * is set, it is implicitly assumed that {@link #ID_TYPE_BASE_INFO} is also set. + * <p> + * Attestation using {@link #ID_TYPE_INDIVIDUAL_ATTESTATION} can only be requested if + * key generation is done in StrongBox. + * * @param admin Which {@link DeviceAdminReceiver} this request is associated with, or * {@code null} if calling from a delegated certificate installer. * @param algorithm The key generation algorithm, see {@link java.security.KeyPairGenerator}. * @param keySpec Specification of the key to generate, see * {@link java.security.KeyPairGenerator}. - * @param idAttestationFlags A bitmask of all the identifiers that should be included in the + * @param idAttestationFlags A bitmask of the identifiers that should be included in the * attestation record ({@code ID_TYPE_BASE_INFO}, {@code ID_TYPE_SERIAL}, - * {@code ID_TYPE_IMEI} and {@code ID_TYPE_MEID}), or {@code 0} if no device - * identification is required in the attestation record. - * Device owner, profile owner and their delegated certificate installer can use - * {@link #ID_TYPE_BASE_INFO} to request inclusion of the general device information - * including manufacturer, model, brand, device and product in the attestation record. - * Only device owner and their delegated certificate installer can use - * {@link #ID_TYPE_SERIAL}, {@link #ID_TYPE_IMEI} and {@link #ID_TYPE_MEID} to request - * unique device identifiers to be attested. + * {@code ID_TYPE_IMEI} and {@code ID_TYPE_MEID}), and + * {@code ID_TYPE_INDIVIDUAL_ATTESTATION} if the attestation record should be signed + * using an individual attestation certificate. * <p> - * If any of {@link #ID_TYPE_SERIAL}, {@link #ID_TYPE_IMEI} and {@link #ID_TYPE_MEID} - * is set, it is implicitly assumed that {@link #ID_TYPE_BASE_INFO} is also set. + * {@code 0} should be passed in if no device identification is required in the + * attestation record and the batch attestation certificate should be used. * <p> * If any flag is specified, then an attestation challenge must be included in the * {@code keySpec}. @@ -5051,7 +5125,8 @@ public class DevicePolicyManager { /** * Returns {@code true} if the device supports attestation of device identifiers in addition - * to key attestation. + * to key attestation. See + * {@link #generateKeyPair(ComponentName, String, KeyGenParameterSpec, int)} * @return {@code true} if Device ID attestation is supported. */ public boolean isDeviceIdAttestationSupported() { @@ -5060,6 +5135,20 @@ public class DevicePolicyManager { } /** + * Returns {@code true} if the StrongBox Keymaster implementation on the device was provisioned + * with an individual attestation certificate and can sign attestation records using it (as + * attestation using an individual attestation certificate is a feature only Keymaster + * implementations with StrongBox security level can implement). + * For use prior to calling + * {@link #generateKeyPair(ComponentName, String, KeyGenParameterSpec, int)}. + * @return {@code true} if individual attestation is supported. + */ + public boolean isUniqueDeviceAttestationSupported() { + PackageManager pm = mContext.getPackageManager(); + return pm.hasSystemFeature(PackageManager.FEATURE_DEVICE_UNIQUE_ATTESTATION); + } + + /** * Called by a device or profile owner, or delegated certificate installer, to associate * certificates with a key pair that was generated using {@link #generateKeyPair}, and * set whether the key is available for the user to choose in the certificate selection diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java index 3418b7be42d6..72204daf01ef 100644 --- a/core/java/android/content/Intent.java +++ b/core/java/android/content/Intent.java @@ -10004,7 +10004,10 @@ public class Intent implements Parcelable, Cloneable { if (!Objects.equals(this.mData, other.mData)) return false; if (!Objects.equals(this.mType, other.mType)) return false; if (!Objects.equals(this.mIdentifier, other.mIdentifier)) return false; - if (!Objects.equals(this.mPackage, other.mPackage)) return false; + if (!(this.hasPackageEquivalentComponent() && other.hasPackageEquivalentComponent()) + && !Objects.equals(this.mPackage, other.mPackage)) { + return false; + } if (!Objects.equals(this.mComponent, other.mComponent)) return false; if (!Objects.equals(this.mCategories, other.mCategories)) return false; @@ -10012,6 +10015,15 @@ public class Intent implements Parcelable, Cloneable { } /** + * Return {@code true} if the component name is not null and is in the same package that this + * intent limited to. otherwise return {@code false}. + */ + private boolean hasPackageEquivalentComponent() { + return mComponent != null + && (mPackage == null || mPackage.equals(mComponent.getPackageName())); + } + + /** * Generate hash code that matches semantics of filterEquals(). * * @return Returns the hash value of the action, data, type, class, and diff --git a/core/java/android/content/pm/AndroidTelephonyCommonUpdater.java b/core/java/android/content/pm/AndroidTelephonyCommonUpdater.java deleted file mode 100644 index 1a720d50f2ce..000000000000 --- a/core/java/android/content/pm/AndroidTelephonyCommonUpdater.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2018 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 android.content.pm; - -import static android.content.pm.SharedLibraryNames.ANDROID_TELEPHONY_COMMON; - - -import com.android.internal.compat.IPlatformCompat; -import android.compat.annotation.ChangeId; -import android.compat.annotation.EnabledAfter; -import android.content.pm.PackageParser.Package; - -import android.os.Build.VERSION_CODES; -import android.os.IBinder; -import android.os.RemoteException; -import android.os.ServiceManager; -import android.util.Log; -import com.android.internal.annotations.VisibleForTesting; - -/** - * Updates a package to ensure that - * <ul> - * <li> if apps have target SDK < R, then telephony-common library is included by default to - * their class path. Even without <uses-library>.</li> - * <li> if apps with target SDK level >= R && have special permission (or Phone UID): - * apply <uses-library> on telephony-common should work.</li> - * <li> Otherwise not allow to use the lib. - * See {@link PackageSharedLibraryUpdater#removeLibrary(Package, String)}.</li> - * </ul> - * - * @hide - */ -@VisibleForTesting -public class AndroidTelephonyCommonUpdater extends PackageSharedLibraryUpdater { - - private static final String TAG = AndroidTelephonyCommonUpdater.class.getSimpleName(); - /** - * Restrict telephony-common lib for apps having target SDK >= R - */ - @ChangeId - @EnabledAfter(targetSdkVersion = VERSION_CODES.Q) - static final long RESTRICT_TELEPHONY_COMMON_CHANGE_ID = 139318877L; - - private static boolean apkTargetsApiLevelLessThanROrCurrent(Package pkg) { - boolean shouldRestrict = false; - try { - IBinder b = ServiceManager.getService("platform_compat"); - IPlatformCompat platformCompat = IPlatformCompat.Stub.asInterface(b); - shouldRestrict = platformCompat.isChangeEnabled(RESTRICT_TELEPHONY_COMMON_CHANGE_ID, - pkg.applicationInfo); - } catch (RemoteException ex) { - Log.e(TAG, ex.getMessage()); - } - // TODO(b/139318877): remove version check for CUR_DEVELOPEMENT after clean up work. - return !shouldRestrict - || pkg.applicationInfo.targetSdkVersion == VERSION_CODES.CUR_DEVELOPMENT; - } - - @Override - public void updatePackage(Package pkg) { - // for apps with targetSDKVersion < R include the library for backward compatibility. - if (apkTargetsApiLevelLessThanROrCurrent(pkg)) { - prefixRequiredLibrary(pkg, ANDROID_TELEPHONY_COMMON); - } else if (pkg.mSharedUserId == null || !pkg.mSharedUserId.equals("android.uid.phone")) { - // if apps target >= R - removeLibrary(pkg, ANDROID_TELEPHONY_COMMON); - } - } -} diff --git a/core/java/android/content/pm/PackageBackwardCompatibility.java b/core/java/android/content/pm/PackageBackwardCompatibility.java index 797ba64b5d1f..4331bd4ac4d4 100644 --- a/core/java/android/content/pm/PackageBackwardCompatibility.java +++ b/core/java/android/content/pm/PackageBackwardCompatibility.java @@ -51,8 +51,6 @@ public class PackageBackwardCompatibility extends PackageSharedLibraryUpdater { packageUpdaters.add(new AndroidHidlUpdater()); - packageUpdaters.add(new AndroidTelephonyCommonUpdater()); - // Add this before adding AndroidTestBaseUpdater so that android.test.base comes before // android.test.mock. packageUpdaters.add(new AndroidTestRunnerSplitUpdater()); diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java index 6d88fea9c4b9..fafb56d20ba0 100644 --- a/core/java/android/content/pm/PackageManager.java +++ b/core/java/android/content/pm/PackageManager.java @@ -2849,6 +2849,17 @@ public abstract class PackageManager { /** * Feature for {@link #getSystemAvailableFeatures} and {@link #hasSystemFeature}: + * The device supports device-unique Keystore attestations. Only available on devices that + * also support {@link #FEATURE_STRONGBOX_KEYSTORE}, and can only be used by device owner + * apps (see {@link android.app.admin.DevicePolicyManager#generateKeyPair}). + * @hide + */ + @SdkConstant(SdkConstantType.FEATURE) + public static final String FEATURE_DEVICE_UNIQUE_ATTESTATION = + "android.hardware.device_unique_attestation"; + + /** + * Feature for {@link #getSystemAvailableFeatures} and {@link #hasSystemFeature}: * The device has a Keymaster implementation that supports Device ID attestation. * * @see DevicePolicyManager#isDeviceIdAttestationSupported diff --git a/core/java/android/content/pm/PackageManagerInternal.java b/core/java/android/content/pm/PackageManagerInternal.java index 3e4649f786e5..f28b85ccbedc 100644 --- a/core/java/android/content/pm/PackageManagerInternal.java +++ b/core/java/android/content/pm/PackageManagerInternal.java @@ -308,6 +308,17 @@ public abstract class PackageManagerInternal { public abstract String getNameForUid(int uid); /** + * Marks a package as installed (or not installed) for a given user. + * + * @param pkg the package whose installation is to be set + * @param userId the user for whom to set it + * @param installed the new installed state + * @return true if the installed state changed as a result + */ + public abstract boolean setInstalled(PackageParser.Package pkg, + @UserIdInt int userId, boolean installed); + + /** * Request to perform the second phase of ephemeral resolution. * @param responseObj The response of the first phase of ephemeral resolution * @param origIntent The original intent that triggered ephemeral resolution diff --git a/core/java/android/content/pm/SharedLibraryNames.java b/core/java/android/content/pm/SharedLibraryNames.java index 4c66fc007856..a607a9ff682b 100644 --- a/core/java/android/content/pm/SharedLibraryNames.java +++ b/core/java/android/content/pm/SharedLibraryNames.java @@ -33,6 +33,4 @@ public class SharedLibraryNames { static final String ANDROID_TEST_RUNNER = "android.test.runner"; public static final String ORG_APACHE_HTTP_LEGACY = "org.apache.http.legacy"; - - public static final String ANDROID_TELEPHONY_COMMON = "telephony-common"; } diff --git a/core/java/android/content/pm/UserInfo.java b/core/java/android/content/pm/UserInfo.java index e65d761cb77e..df652f190d04 100644 --- a/core/java/android/content/pm/UserInfo.java +++ b/core/java/android/content/pm/UserInfo.java @@ -126,6 +126,13 @@ public class UserInfo implements Parcelable { public static final int FLAG_SYSTEM = 0x00000800; /** + * Indicates that this user is some sort of profile. Right now, the only profile type is + * {@link #FLAG_MANAGED_PROFILE}, but this can include other types of profiles too if any + * are created in the future. This is therefore not a flag, but an OR of several flags. + */ + public static final int PROFILE_FLAGS_MASK = FLAG_MANAGED_PROFILE; + + /** * @hide */ @IntDef(flag = true, prefix = "FLAG_", value = { diff --git a/core/java/android/content/res/AssetManager.java b/core/java/android/content/res/AssetManager.java index 2420a6109155..567e26b4c2f6 100644 --- a/core/java/android/content/res/AssetManager.java +++ b/core/java/android/content/res/AssetManager.java @@ -1376,7 +1376,6 @@ public final class AssetManager implements AutoCloseable { /** * @hide */ - @TestApi @GuardedBy("this") public @Nullable Map<String, String> getOverlayableMap(String packageName) { synchronized (this) { @@ -1385,6 +1384,18 @@ public final class AssetManager implements AutoCloseable { } } + /** + * @hide + */ + @TestApi + @GuardedBy("this") + public @Nullable String getOverlayablesToString(String packageName) { + synchronized (this) { + ensureValidLocked(); + return nativeGetOverlayablesToString(mObject, packageName); + } + } + @GuardedBy("this") private void incRefsLocked(long id) { if (DEBUG_REFS) { @@ -1504,6 +1515,8 @@ public final class AssetManager implements AutoCloseable { private static native String[] nativeCreateIdmapsForStaticOverlaysTargetingAndroid(); private static native @Nullable Map nativeGetOverlayableMap(long ptr, @NonNull String packageName); + private static native @Nullable String nativeGetOverlayablesToString(long ptr, + @NonNull String packageName); // Global debug native methods. /** diff --git a/core/java/android/view/CompositionSamplingListener.java b/core/java/android/view/CompositionSamplingListener.java index 368445cde72c..677a559cd3b0 100644 --- a/core/java/android/view/CompositionSamplingListener.java +++ b/core/java/android/view/CompositionSamplingListener.java @@ -28,7 +28,7 @@ import java.util.concurrent.Executor; */ public abstract class CompositionSamplingListener { - private final long mNativeListener; + private long mNativeListener; private final Executor mExecutor; public CompositionSamplingListener(Executor executor) { @@ -36,13 +36,19 @@ public abstract class CompositionSamplingListener { mNativeListener = nativeCreate(this); } + public void destroy() { + if (mNativeListener == 0) { + return; + } + unregister(this); + nativeDestroy(mNativeListener); + mNativeListener = 0; + } + @Override protected void finalize() throws Throwable { try { - if (mNativeListener != 0) { - unregister(this); - nativeDestroy(mNativeListener); - } + destroy(); } finally { super.finalize(); } @@ -58,6 +64,9 @@ public abstract class CompositionSamplingListener { */ public static void register(CompositionSamplingListener listener, int displayId, SurfaceControl stopLayer, Rect samplingArea) { + if (listener.mNativeListener == 0) { + return; + } Preconditions.checkArgument(displayId == Display.DEFAULT_DISPLAY, "default display only for now"); long nativeStopLayerObject = stopLayer != null ? stopLayer.mNativeObject : 0; @@ -69,6 +78,9 @@ public abstract class CompositionSamplingListener { * Unregisters a sampling listener. */ public static void unregister(CompositionSamplingListener listener) { + if (listener.mNativeListener == 0) { + return; + } nativeUnregister(listener.mNativeListener); } diff --git a/core/java/android/view/IPinnedStackListener.aidl b/core/java/android/view/IPinnedStackListener.aidl index 806d81e39cca..f4bee575f811 100644 --- a/core/java/android/view/IPinnedStackListener.aidl +++ b/core/java/android/view/IPinnedStackListener.aidl @@ -55,14 +55,6 @@ oneway interface IPinnedStackListener { void onImeVisibilityChanged(boolean imeVisible, int imeHeight); /** - * Called when window manager decides to adjust the pinned stack bounds because of the shelf, or - * when the listener is first registered to allow the listener to synchronized its state with - * the controller. This call will always be followed by a onMovementBoundsChanged() call - * with fromShelfAdjustment set to {@code true}. - */ - void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight); - - /** * Called when window manager decides to adjust the minimized state, or when the listener * is first registered to allow the listener to synchronized its state with the controller. */ diff --git a/core/java/android/view/IWindowManager.aidl b/core/java/android/view/IWindowManager.aidl index 1c3294858db8..49e8800a36c6 100644 --- a/core/java/android/view/IWindowManager.aidl +++ b/core/java/android/view/IWindowManager.aidl @@ -326,12 +326,6 @@ interface IWindowManager oneway void setPipVisibility(boolean visible); /** - * Called by System UI to notify of changes to the visibility and height of the shelf. - */ - @UnsupportedAppUsage - void setShelfHeight(boolean visible, int shelfHeight); - - /** * Called by System UI to enable or disable haptic feedback on the navigation bar buttons. */ @UnsupportedAppUsage diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java index db3ef20d5859..06ff568202d5 100644 --- a/core/java/android/view/SurfaceView.java +++ b/core/java/android/view/SurfaceView.java @@ -1128,11 +1128,12 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall return; } - if (frameNumber > 0) { - final ViewRootImpl viewRoot = getViewRootImpl(); - - mRtTransaction.deferTransactionUntilSurface(mSurfaceControl, viewRoot.mSurface, - frameNumber); + final ViewRootImpl viewRoot = getViewRootImpl(); + if (frameNumber > 0 && viewRoot != null) { + if (viewRoot.mSurface.isValid()) { + mRtTransaction.deferTransactionUntilSurface(mSurfaceControl, viewRoot.mSurface, + frameNumber); + } } mRtTransaction.hide(mSurfaceControl); diff --git a/core/java/com/android/internal/compat/ChangeReporter.java b/core/java/com/android/internal/compat/ChangeReporter.java index 1ce071bd005a..5ea970d4c746 100644 --- a/core/java/com/android/internal/compat/ChangeReporter.java +++ b/core/java/com/android/internal/compat/ChangeReporter.java @@ -16,14 +16,89 @@ package com.android.internal.compat; +import android.util.Log; +import android.util.Slog; import android.util.StatsLog; +import com.android.internal.annotations.GuardedBy; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + /** * A helper class to report changes to stats log. * * @hide */ public final class ChangeReporter { + private static final String TAG = "CompatibilityChangeReporter"; + private int mSource; + + private final class ChangeReport { + int mUid; + long mChangeId; + int mState; + + ChangeReport(int uid, long changeId, int state) { + mUid = uid; + mChangeId = changeId; + mState = state; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ChangeReport that = (ChangeReport) o; + return mUid == that.mUid + && mChangeId == that.mChangeId + && mState == that.mState; + } + + @Override + public int hashCode() { + return Objects.hash(mUid, mChangeId, mState); + } + } + + @GuardedBy("mReportedChanges") + private Set<ChangeReport> mReportedChanges = new HashSet<>(); + + public ChangeReporter(int source) { + mSource = source; + } + + /** + * Report the change to stats log. + * + * @param uid affected by the change + * @param changeId the reported change id + * @param state of the reported change - enabled/disabled/only logged + */ + public void reportChange(int uid, long changeId, int state) { + debugLog(uid, changeId, state); + ChangeReport report = new ChangeReport(uid, changeId, state); + synchronized (mReportedChanges) { + if (!mReportedChanges.contains(report)) { + StatsLog.write(StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED, uid, changeId, + state, mSource); + mReportedChanges.add(report); + } + } + } + + private void debugLog(int uid, long changeId, int state) { + //TODO(b/138374585): Implement rate limiting for the logs. + String message = String.format("Compat change id reported: %d; UID %d; state: %s", changeId, + uid, stateToString(state)); + if (mSource == StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER) { + Slog.d(TAG, message); + } else { + Log.d(TAG, message); + } + + } /** * Transforms StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE enum to a string. @@ -43,31 +118,4 @@ public final class ChangeReporter { return "UNKNOWN"; } } - - /** - * Constructs and returns a string to be logged to logcat when a change is reported. - * - * @param uid affected by the change - * @param changeId the reported change id - * @param state of the reported change - enabled/disabled/only logged - * @return string to log - */ - public static String createLogString(int uid, long changeId, int state) { - return String.format("Compat change id reported: %d; UID %d; state: %s", changeId, uid, - stateToString(state)); - } - - /** - * Report the change to stats log. - * - * @param uid affected by the change - * @param changeId the reported change id - * @param state of the reported change - enabled/disabled/only logged - * @param source of the logging - app process or system server - */ - public void reportChange(int uid, long changeId, int state, int source) { - //TODO(b/138374585): Implement rate limiting for stats log. - StatsLog.write(StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED, uid, changeId, - state, source); - } } diff --git a/core/java/com/android/internal/compat/IPlatformCompat.aidl b/core/java/com/android/internal/compat/IPlatformCompat.aidl index 9049c3aea7ec..e415b41459ab 100644 --- a/core/java/com/android/internal/compat/IPlatformCompat.aidl +++ b/core/java/com/android/internal/compat/IPlatformCompat.aidl @@ -33,15 +33,30 @@ interface IPlatformCompat * Reports that a compatibility change is affecting an app process now. * * <p>Note: for changes that are gated using {@link #isChangeEnabled(long, ApplicationInfo)}, - * you do not need to call this API directly. The change will be reported for you in the case - * that {@link #isChangeEnabled(long, ApplicationInfo)} returns {@code true}. + * you do not need to call this API directly. The change will be reported for you. * * @param changeId The ID of the compatibility change taking effect. - * @param appInfo Representing the affected app. + * @param appInfo Representing the affected app. */ void reportChange(long changeId, in ApplicationInfo appInfo); /** + * Reports that a compatibility change is affecting an app process now. + * + * <p>Same as {@link #reportChange(long, ApplicationInfo)}, except it receives a package name + * instead of an {@link ApplicationInfo} + * object, and finds an app info object based on the package name. Returns {@code true} if + * there is no installed package by that name. + * + * <p>Note: for changes that are gated using {@link #isChangeEnabled(long, String)}, + * you do not need to call this API directly. The change will be reported for you. + * + * @param changeId The ID of the compatibility change taking effect. + * @param packageName The package name of the app in question. + */ + void reportChangeByPackageName(long changeId, in String packageName); + + /** * Query if a given compatibility change is enabled for an app process. This method should * be called when implementing functionality on behalf of the affected app. * @@ -49,13 +64,35 @@ interface IPlatformCompat * change, resulting in differing behaviour compared to earlier releases. If this method returns * {@code false}, the calling code should behave as it did in earlier releases. * - * <p>When this method returns {@code true}, it will also report the change as - * {@link #reportChange(long, ApplicationInfo)} would, so there is no need to call that method - * directly. + * <p>It will also report the change as {@link #reportChange(long, ApplicationInfo)} would, so + * there is no need to call that method directly. * * @param changeId The ID of the compatibility change in question. - * @param appInfo Representing the app in question. + * @param appInfo Representing the app in question. * @return {@code true} if the change is enabled for the current app. */ boolean isChangeEnabled(long changeId, in ApplicationInfo appInfo); + + /** + * Query if a given compatibility change is enabled for an app process. This method should + * be called when implementing functionality on behalf of the affected app. + * + * <p>Same as {@link #isChangeEnabled(long, ApplicationInfo)}, except it receives a package name + * instead of an {@link ApplicationInfo} + * object, and finds an app info object based on the package name. Returns {@code true} if + * there is no installed package by that name. + * + * <p>If this method returns {@code true}, the calling code should implement the compatibility + * change, resulting in differing behaviour compared to earlier releases. If this method + * returns + * {@code false}, the calling code should behave as it did in earlier releases. + * + * <p>It will also report the change as {@link #reportChange(long, String)} would, so there is + * no need to call that method directly. + * + * @param changeId The ID of the compatibility change in question. + * @param packageName The package name of the app in question. + * @return {@code true} if the change is enabled for the current app. + */ + boolean isChangeEnabledByPackageName(long changeId, in String packageName); }
\ No newline at end of file diff --git a/core/java/com/android/internal/infra/ServiceConnector.java b/core/java/com/android/internal/infra/ServiceConnector.java index d6862f0188ce..98d679eb776b 100644 --- a/core/java/com/android/internal/infra/ServiceConnector.java +++ b/core/java/com/android/internal/infra/ServiceConnector.java @@ -32,6 +32,7 @@ import android.text.TextUtils; import android.util.DebugUtils; import android.util.Log; +import com.android.internal.util.Preconditions; import com.android.internal.util.function.pooled.PooledLambda; import java.io.PrintWriter; @@ -351,7 +352,7 @@ public interface ServiceConnector<I extends IInterface> { @Override public <R> CompletionAwareJob<I, R> postForResult(@NonNull Job<I, R> job) { CompletionAwareJob<I, R> task = new CompletionAwareJob<>(); - task.mDelegate = job; + task.mDelegate = Preconditions.checkNotNull(job); enqueue(task); return task; } @@ -359,7 +360,7 @@ public interface ServiceConnector<I extends IInterface> { @Override public <R> AndroidFuture<R> postAsync(@NonNull Job<I, CompletableFuture<R>> job) { CompletionAwareJob<I, R> task = new CompletionAwareJob<>(); - task.mDelegate = (Job) job; + task.mDelegate = Preconditions.checkNotNull((Job) job); task.mAsync = true; enqueue(task); return task; diff --git a/core/java/com/android/internal/os/RuntimeInit.java b/core/java/com/android/internal/os/RuntimeInit.java index 1de2e7272f4d..d6caa0930243 100644 --- a/core/java/com/android/internal/os/RuntimeInit.java +++ b/core/java/com/android/internal/os/RuntimeInit.java @@ -192,6 +192,15 @@ public class RuntimeInit { } } + /** + * Common initialization that (unlike {@link #commonInit()} should happen prior to + * the Zygote fork. + */ + public static void preForkInit() { + if (DEBUG) Slog.d(TAG, "Entered preForkInit."); + RuntimeInit.enableDdms(); + } + @UnsupportedAppUsage protected static final void commonInit() { if (DEBUG) Slog.d(TAG, "Entered RuntimeInit!"); @@ -324,7 +333,7 @@ public class RuntimeInit { @UnsupportedAppUsage public static final void main(String[] argv) { - enableDdms(); + preForkInit(); if (argv.length == 2 && argv[1].equals("application")) { if (DEBUG) Slog.d(TAG, "RuntimeInit: Starting application"); redirectLogStreams(); @@ -418,7 +427,7 @@ public class RuntimeInit { /** * Enable DDMS. */ - static final void enableDdms() { + private static void enableDdms() { // Register handlers for DDM messages. android.ddm.DdmRegister.registerHandlers(); } diff --git a/core/java/com/android/internal/os/ZygoteInit.java b/core/java/com/android/internal/os/ZygoteInit.java index 3be1a1aefe57..158700b2a449 100644 --- a/core/java/com/android/internal/os/ZygoteInit.java +++ b/core/java/com/android/internal/os/ZygoteInit.java @@ -847,7 +847,7 @@ public class ZygoteInit { TimingsTraceLog bootTimingsTraceLog = new TimingsTraceLog(bootTimeTag, Trace.TRACE_TAG_DALVIK); bootTimingsTraceLog.traceBegin("ZygoteInit"); - RuntimeInit.enableDdms(); + RuntimeInit.preForkInit(); boolean startSystemServer = false; String zygoteSocketName = "zygote"; diff --git a/core/java/com/android/server/SystemConfig.java b/core/java/com/android/server/SystemConfig.java index 7cd3e95c6499..697825dcefd7 100644 --- a/core/java/com/android/server/SystemConfig.java +++ b/core/java/com/android/server/SystemConfig.java @@ -35,6 +35,7 @@ import android.util.Slog; import android.util.SparseArray; import android.util.Xml; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.XmlUtils; import libcore.io.IoUtils; @@ -50,6 +51,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Set; /** * Loads global system configuration info. @@ -209,6 +211,10 @@ public class SystemConfig { private final ArraySet<String> mBugreportWhitelistedPackages = new ArraySet<>(); + // Map of packagesNames to userTypes. Stored temporarily until cleared by UserManagerService(). + private ArrayMap<String, Set<String>> mPackageToUserTypeWhitelist = new ArrayMap<>(); + private ArrayMap<String, Set<String>> mPackageToUserTypeBlacklist = new ArrayMap<>(); + public static SystemConfig getInstance() { if (!isSystemProcess()) { Slog.wtf(TAG, "SystemConfig is being accessed by a process other than " @@ -359,7 +365,48 @@ public class SystemConfig { return mBugreportWhitelistedPackages; } + /** + * Gets map of packagesNames to userTypes, dictating on which user types each package should be + * initially installed, and then removes this map from SystemConfig. + * Called by UserManagerService when it is constructed. + */ + public ArrayMap<String, Set<String>> getAndClearPackageToUserTypeWhitelist() { + ArrayMap<String, Set<String>> r = mPackageToUserTypeWhitelist; + mPackageToUserTypeWhitelist = new ArrayMap<>(0); + return r; + } + + /** + * Gets map of packagesNames to userTypes, dictating on which user types each package should NOT + * be initially installed, even if they are whitelisted, and then removes this map from + * SystemConfig. + * Called by UserManagerService when it is constructed. + */ + public ArrayMap<String, Set<String>> getAndClearPackageToUserTypeBlacklist() { + ArrayMap<String, Set<String>> r = mPackageToUserTypeBlacklist; + mPackageToUserTypeBlacklist = new ArrayMap<>(0); + return r; + } + + /** + * Only use for testing. Do NOT use in production code. + * @param readPermissions false to create an empty SystemConfig; true to read the permissions. + */ + @VisibleForTesting + protected SystemConfig(boolean readPermissions) { + if (readPermissions) { + Slog.w(TAG, "Constructing a test SystemConfig"); + readAllPermissions(); + } else { + Slog.w(TAG, "Constructing an empty test SystemConfig"); + } + } + SystemConfig() { + readAllPermissions(); + } + + private void readAllPermissions() { // Read configuration from system readPermissions(Environment.buildPath( Environment.getRootDirectory(), "etc", "sysconfig"), ALLOW_ALL); @@ -419,7 +466,8 @@ public class SystemConfig { Environment.getSystemExtDirectory(), "etc", "permissions"), ALLOW_ALL); } - void readPermissions(File libraryDir, int permissionFlag) { + @VisibleForTesting + public void readPermissions(File libraryDir, int permissionFlag) { // Read permissions from given directory. if (!libraryDir.exists() || !libraryDir.isDirectory()) { if (permissionFlag == ALLOW_ALL) { @@ -954,6 +1002,11 @@ public class SystemConfig { } XmlUtils.skipCurrentTag(parser); } break; + case "install-in-user-type": { + // NB: We allow any directory permission to declare install-in-user-type. + readInstallInUserType(parser, + mPackageToUserTypeWhitelist, mPackageToUserTypeBlacklist); + } break; default: { Slog.w(TAG, "Tag " + name + " is unknown in " + permFile + " at " + parser.getPositionDescription()); @@ -1091,6 +1144,53 @@ public class SystemConfig { } } + private void readInstallInUserType(XmlPullParser parser, + Map<String, Set<String>> doInstallMap, + Map<String, Set<String>> nonInstallMap) + throws IOException, XmlPullParserException { + final String packageName = parser.getAttributeValue(null, "package"); + if (TextUtils.isEmpty(packageName)) { + Slog.w(TAG, "package is required for <install-in-user-type> in " + + parser.getPositionDescription()); + return; + } + + Set<String> userTypesYes = doInstallMap.get(packageName); + Set<String> userTypesNo = nonInstallMap.get(packageName); + final int depth = parser.getDepth(); + while (XmlUtils.nextElementWithin(parser, depth)) { + final String name = parser.getName(); + if ("install-in".equals(name)) { + final String userType = parser.getAttributeValue(null, "user-type"); + if (TextUtils.isEmpty(userType)) { + Slog.w(TAG, "user-type is required for <install-in-user-type> in " + + parser.getPositionDescription()); + continue; + } + if (userTypesYes == null) { + userTypesYes = new ArraySet<>(); + doInstallMap.put(packageName, userTypesYes); + } + userTypesYes.add(userType); + } else if ("do-not-install-in".equals(name)) { + final String userType = parser.getAttributeValue(null, "user-type"); + if (TextUtils.isEmpty(userType)) { + Slog.w(TAG, "user-type is required for <install-in-user-type> in " + + parser.getPositionDescription()); + continue; + } + if (userTypesNo == null) { + userTypesNo = new ArraySet<>(); + nonInstallMap.put(packageName, userTypesNo); + } + userTypesNo.add(userType); + } else { + Slog.w(TAG, "unrecognized tag in <install-in-user-type> in " + + parser.getPositionDescription()); + } + } + } + void readOemPermissions(XmlPullParser parser) throws IOException, XmlPullParserException { final String packageName = parser.getAttributeValue(null, "package"); if (TextUtils.isEmpty(packageName)) { diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp index b4374fe26576..d46fe8d3388a 100644 --- a/core/jni/AndroidRuntime.cpp +++ b/core/jni/AndroidRuntime.cpp @@ -696,26 +696,32 @@ int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv, bool zygote, bool p // Read if we are using the profile configuration, do this at the start since the last ART args // take precedence. property_get("dalvik.vm.profilebootclasspath", propBuf, ""); - std::string profile_boot_class_path = propBuf; + std::string profile_boot_class_path_flag = propBuf; // Empty means the property is unset and we should default to the phenotype property. // The possible values are {"true", "false", ""} - if (profile_boot_class_path.empty()) { - profile_boot_class_path = server_configurable_flags::GetServerConfigurableFlag( + if (profile_boot_class_path_flag.empty()) { + profile_boot_class_path_flag = server_configurable_flags::GetServerConfigurableFlag( RUNTIME_NATIVE_BOOT_NAMESPACE, PROFILE_BOOT_CLASS_PATH, /*default_value=*/ ""); } - if (profile_boot_class_path == "true") { + const bool profile_boot_class_path = (profile_boot_class_path_flag == "true"); + if (profile_boot_class_path) { + addOption("-Xcompiler-option"); + addOption("--count-hotness-in-compiled-code"); addOption("-Xps-profile-boot-class-path"); addOption("-Xps-profile-aot-code"); addOption("-Xjitsaveprofilinginfo"); } - std::string use_apex_image = + std::string use_apex_image_flag = server_configurable_flags::GetServerConfigurableFlag(RUNTIME_NATIVE_BOOT_NAMESPACE, ENABLE_APEX_IMAGE, /*default_value=*/ ""); - if (use_apex_image == "true") { + // Use the APEX boot image for boot class path profiling to get JIT samples on BCP methods. + // Also use the APEX boot image if it's explicitly enabled via configuration flag. + const bool use_apex_image = profile_boot_class_path || (use_apex_image_flag == "true"); + if (use_apex_image) { addOption(kApexImageOption); ALOGI("Using Apex boot image: '%s'\n", kApexImageOption); } else if (parseRuntimeOption("dalvik.vm.boot-image", bootImageBuf, "-Ximage:")) { diff --git a/core/jni/android/graphics/Bitmap.cpp b/core/jni/android/graphics/Bitmap.cpp index 18a1b43d3f5f..89c12f88594d 100755 --- a/core/jni/android/graphics/Bitmap.cpp +++ b/core/jni/android/graphics/Bitmap.cpp @@ -265,6 +265,20 @@ void imageInfo(JNIEnv* env, jobject bitmap, AndroidBitmapInfo* info) { info->format = ANDROID_BITMAP_FORMAT_NONE; break; } + switch (imageInfo.alphaType()) { + case kUnknown_SkAlphaType: + LOG_ALWAYS_FATAL("Bitmap has no alpha type"); + break; + case kOpaque_SkAlphaType: + info->flags |= ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE; + break; + case kPremul_SkAlphaType: + info->flags |= ANDROID_BITMAP_FLAGS_ALPHA_PREMUL; + break; + case kUnpremul_SkAlphaType: + info->flags |= ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL; + break; + } } void* lockPixels(JNIEnv* env, jobject bitmap) { diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp index 9c52a6433360..f2a4f4fd203c 100644 --- a/core/jni/android_os_Debug.cpp +++ b/core/jni/android_os_Debug.cpp @@ -257,6 +257,8 @@ static void load_maps(int pid, stats_t* stats, bool* foundSwapPss) which_heap = HEAP_NATIVE; } else if (base::StartsWith(name, "[anon:libc_malloc]")) { which_heap = HEAP_NATIVE; + } else if (base::StartsWith(name, "[anon:scudo:")) { + which_heap = HEAP_NATIVE; } else if (base::StartsWith(name, "[stack")) { which_heap = HEAP_STACK; } else if (base::StartsWith(name, "[anon:stack_and_tls:")) { diff --git a/core/jni/android_util_AssetManager.cpp b/core/jni/android_util_AssetManager.cpp index bf4ffc7e42e0..daf33f61105c 100644 --- a/core/jni/android_util_AssetManager.cpp +++ b/core/jni/android_util_AssetManager.cpp @@ -352,7 +352,7 @@ static Guarded<AssetManager2>& AssetManagerFromLong(jlong ptr) { } static jobject NativeGetOverlayableMap(JNIEnv* env, jclass /*clazz*/, jlong ptr, - jstring package_name) { + jstring package_name) { ScopedLock<AssetManager2> assetmanager(AssetManagerFromLong(ptr)); const ScopedUtfChars package_name_utf8(env, package_name); CHECK(package_name_utf8.c_str() != nullptr); @@ -397,6 +397,21 @@ static jobject NativeGetOverlayableMap(JNIEnv* env, jclass /*clazz*/, jlong ptr, return array_map; } +static jstring NativeGetOverlayablesToString(JNIEnv* env, jclass /*clazz*/, jlong ptr, + jstring package_name) { + ScopedLock<AssetManager2> assetmanager(AssetManagerFromLong(ptr)); + const ScopedUtfChars package_name_utf8(env, package_name); + CHECK(package_name_utf8.c_str() != nullptr); + const std::string std_package_name(package_name_utf8.c_str()); + + std::string result; + if (!assetmanager->GetOverlayablesToString(std_package_name, &result)) { + return nullptr; + } + + return env->NewStringUTF(result.c_str()); +} + #ifdef __ANDROID__ // Layoutlib does not support parcel static jobject ReturnParcelFileDescriptor(JNIEnv* env, std::unique_ptr<Asset> asset, jlongArray out_offsets) { @@ -1608,6 +1623,8 @@ static const JNINativeMethod gAssetManagerMethods[] = { (void*)NativeCreateIdmapsForStaticOverlaysTargetingAndroid}, {"nativeGetOverlayableMap", "(JLjava/lang/String;)Ljava/util/Map;", (void*)NativeGetOverlayableMap}, + {"nativeGetOverlayablesToString", "(JLjava/lang/String;)Ljava/lang/String;", + (void*)NativeGetOverlayablesToString}, // Global management/debug methods. {"getGlobalAssetCount", "()I", (void*)NativeGetGlobalAssetCount}, diff --git a/core/jni/com_android_internal_os_Zygote.cpp b/core/jni/com_android_internal_os_Zygote.cpp index 93ef75148df7..3516dce5d5ed 100644 --- a/core/jni/com_android_internal_os_Zygote.cpp +++ b/core/jni/com_android_internal_os_Zygote.cpp @@ -74,7 +74,6 @@ #include <android-base/strings.h> #include <android-base/unique_fd.h> #include <bionic/malloc.h> -#include <cutils/ashmem.h> #include <cutils/fs.h> #include <cutils/multiuser.h> #include <cutils/sockets.h> @@ -1657,11 +1656,6 @@ static void com_android_internal_os_Zygote_nativeInitNativeState(JNIEnv* env, jc if (!SetTaskProfiles(0, {})) { ZygoteFailure(env, "zygote", nullptr, "Zygote SetTaskProfiles failed"); } - - /* - * ashmem initialization to avoid dlopen overhead - */ - ashmem_init(); } /** diff --git a/core/proto/android/server/protolog.proto b/core/proto/android/server/protolog.proto index 7c98d318570b..3512c0aea4a5 100644 --- a/core/proto/android/server/protolog.proto +++ b/core/proto/android/server/protolog.proto @@ -23,7 +23,7 @@ option java_multiple_files = true; /* represents a single log entry */ message ProtoLogMessage { /* log statement identifier, created from message string and log level. */ - optional fixed32 message_hash = 1; + optional sfixed32 message_hash = 1; /* log time, relative to the elapsed system time clock. */ optional fixed64 elapsed_realtime_nanos = 2; /* string parameters passed to the log call. */ diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index 5fd53de9abc4..e3337b7f9ae5 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -2527,6 +2527,16 @@ will be locked. --> <bool name="config_multiuserDelayUserDataLocking">false</bool> + <!-- Whether to only install system packages on a user if they're whitelisted for that user + type. These are flags and can be freely combined. + 0 (0b000) - disable whitelist (install all system packages; no logging) + 1 (0b001) - enforce (only install system packages if they are whitelisted) + 2 (0b010) - log (log when a non-whitelisted package is run) + 4 (0b100) - treat any package not mentioned in the whitelist file as implicitly whitelisted + Note: This list must be kept current with PACKAGE_WHITELIST_MODE_PROP in + frameworks/base/services/core/java/com/android/server/pm/UserSystemPackageInstaller.java --> + <integer name="config_userTypePackageWhitelistMode">5</integer> <!-- 0b101 --> + <!-- Whether UI for multi user should be shown --> <bool name="config_enableMultiUserUI">false</bool> diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml index 56e1fed5bcec..b53a399f0c8a 100644 --- a/core/res/res/values/strings.xml +++ b/core/res/res/values/strings.xml @@ -3634,6 +3634,11 @@ <!-- Message of notification shown when Test Harness Mode is enabled. [CHAR LIMIT=NONE] --> <string name="test_harness_mode_notification_message">Perform a factory reset to disable Test Harness Mode.</string> + <!-- Title of notification shown when serial console is enabled. [CHAR LIMIT=NONE] --> + <string name="console_running_notification_title">Serial console enabled</string> + <!-- Message of notification shown when serial console is enabled. [CHAR LIMIT=NONE] --> + <string name="console_running_notification_message">Performance is impacted. To disable, check bootloader.</string> + <!-- Title of notification shown when contaminant is detected on the USB port. [CHAR LIMIT=NONE] --> <string name="usb_contaminant_detected_title">Liquid or debris in USB port</string> <!-- Message of notification shown when contaminant is detected on the USB port. [CHAR LIMIT=NONE] --> diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 761e02fe5dad..3d0a3b309720 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -511,6 +511,7 @@ <java-symbol type="integer" name="config_multiuserMaximumUsers" /> <java-symbol type="integer" name="config_multiuserMaxRunningUsers" /> <java-symbol type="bool" name="config_multiuserDelayUserDataLocking" /> + <java-symbol type="integer" name="config_userTypePackageWhitelistMode"/> <java-symbol type="integer" name="config_safe_media_volume_index" /> <java-symbol type="integer" name="config_safe_media_volume_usb_mB" /> <java-symbol type="integer" name="config_mobile_mtu" /> @@ -2093,6 +2094,8 @@ <java-symbol type="string" name="adb_active_notification_title" /> <java-symbol type="string" name="test_harness_mode_notification_title" /> <java-symbol type="string" name="test_harness_mode_notification_message" /> + <java-symbol type="string" name="console_running_notification_title" /> + <java-symbol type="string" name="console_running_notification_message" /> <java-symbol type="string" name="taking_remote_bugreport_notification_title" /> <java-symbol type="string" name="share_remote_bugreport_notification_title" /> <java-symbol type="string" name="sharing_remote_bugreport_notification_title" /> diff --git a/core/tests/coretests/src/android/content/pm/AndroidTelephonyCommonUpdaterTest.java b/core/tests/coretests/src/android/content/pm/AndroidTelephonyCommonUpdaterTest.java deleted file mode 100644 index 8ab9ddbee6d8..000000000000 --- a/core/tests/coretests/src/android/content/pm/AndroidTelephonyCommonUpdaterTest.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (C) 2018 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 android.content.pm; - -import static android.content.pm.PackageBuilder.builder; -import static android.content.pm.SharedLibraryNames.ANDROID_HIDL_BASE; -import static android.content.pm.SharedLibraryNames.ANDROID_HIDL_MANAGER; -import static android.content.pm.SharedLibraryNames.ANDROID_TELEPHONY_COMMON; - -import android.os.Build; -import androidx.test.filters.SmallTest; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Test for {@link AndroidHidlUpdater} - */ -@SmallTest -@RunWith(JUnit4.class) -public class AndroidTelephonyCommonUpdaterTest extends PackageSharedLibraryUpdaterTest { - - private static final String OTHER_LIBRARY = "other.library"; - private static final String PHONE_UID = "android.uid.phone"; - - @Test - public void targeted_at_Q() { - PackageBuilder before = builder() - .targetSdkVersion(Build.VERSION_CODES.Q); - - PackageBuilder after = builder().targetSdkVersion(Build.VERSION_CODES.Q) - .requiredLibraries(ANDROID_TELEPHONY_COMMON); - - // Should add telephony-common libraries - checkBackwardsCompatibility(before, after); - } - - @Test - public void targeted_at_Q_phoneUID() { - PackageBuilder before = builder().setSharedUid(PHONE_UID) - .targetSdkVersion(Build.VERSION_CODES.Q); - - // Should add telephony-common libraries - PackageBuilder after = builder().setSharedUid(PHONE_UID) - .targetSdkVersion(Build.VERSION_CODES.Q) - .requiredLibraries(ANDROID_TELEPHONY_COMMON); - - checkBackwardsCompatibility(before, after); - } - - @Test - public void targeted_at_Q_not_empty_usesLibraries() { - PackageBuilder before = builder() - .targetSdkVersion(Build.VERSION_CODES.Q) - .requiredLibraries(OTHER_LIBRARY); - - // no change - checkBackwardsCompatibility(before, before); - } - - @Test - public void targeted_at_Q_not_empty_usesLibraries_phoneUID() { - PackageBuilder before = builder().setSharedUid(PHONE_UID) - .targetSdkVersion(Build.VERSION_CODES.Q) - .requiredLibraries(OTHER_LIBRARY); - - // The telephony-common jars should be added at the start of the list because it - // is not on the bootclasspath and the package targets pre-R. - PackageBuilder after = builder().setSharedUid(PHONE_UID) - .targetSdkVersion(Build.VERSION_CODES.Q) - .requiredLibraries(ANDROID_TELEPHONY_COMMON, OTHER_LIBRARY); - - checkBackwardsCompatibility(before, after); - } - - @Test - public void targeted_at_R_in_usesLibraries() { - PackageBuilder before = builder() - .targetSdkVersion(Build.VERSION_CODES.Q + 1) - .requiredLibraries(ANDROID_TELEPHONY_COMMON); - - PackageBuilder after = builder() - .targetSdkVersion(Build.VERSION_CODES.Q + 1); - - // Libraries are removed because they are not available for apps target >= R and not run - // on phone-uid - checkBackwardsCompatibility(before, after); - } - - @Test - public void targeted_at_Q_in_usesLibraries() { - PackageBuilder before = builder().asSystemApp() - .targetSdkVersion(Build.VERSION_CODES.Q) - .requiredLibraries(ANDROID_TELEPHONY_COMMON); - - // No change is required because the package explicitly requests the telephony libraries - // and is targeted at the current version so does not need backwards compatibility. - checkBackwardsCompatibility(before, before); - } - - - @Test - public void targeted_at_R_in_usesOptionalLibraries() { - PackageBuilder before = builder().targetSdkVersion(Build.VERSION_CODES.Q + 1) - .optionalLibraries(ANDROID_TELEPHONY_COMMON); - - // Dependency is removed, it is not available. - PackageBuilder after = builder().targetSdkVersion(Build.VERSION_CODES.Q + 1); - - // Libraries are removed because they are not available for apps targeting Q+ - checkBackwardsCompatibility(before, after); - } - - @Test - public void targeted_at_R() { - PackageBuilder before = builder() - .targetSdkVersion(Build.VERSION_CODES.Q + 1); - - // no change - checkBackwardsCompatibility(before, before); - } - - private void checkBackwardsCompatibility(PackageBuilder before, PackageBuilder after) { - checkBackwardsCompatibility(before, after, AndroidTelephonyCommonUpdater::new); - } -} diff --git a/core/tests/coretests/src/android/content/pm/PackageBuilder.java b/core/tests/coretests/src/android/content/pm/PackageBuilder.java index f3a56e2814e4..f7544af43461 100644 --- a/core/tests/coretests/src/android/content/pm/PackageBuilder.java +++ b/core/tests/coretests/src/android/content/pm/PackageBuilder.java @@ -37,8 +37,6 @@ class PackageBuilder { private ArrayList<String> mOptionalLibraries; - private String mSharedUid; - public static PackageBuilder builder() { return new PackageBuilder(); } @@ -49,7 +47,6 @@ class PackageBuilder { pkg.applicationInfo.flags = mFlags; pkg.usesLibraries = mRequiredLibraries; pkg.usesOptionalLibraries = mOptionalLibraries; - pkg.mSharedUserId = mSharedUid; return pkg; } @@ -58,11 +55,6 @@ class PackageBuilder { return this; } - PackageBuilder setSharedUid(String uid) { - this.mSharedUid = uid; - return this; - } - PackageBuilder asSystemApp() { this.mFlags |= ApplicationInfo.FLAG_SYSTEM; return this; diff --git a/data/etc/Android.bp b/data/etc/Android.bp index 7e167c9bfb79..befa63760a49 100644 --- a/data/etc/Android.bp +++ b/data/etc/Android.bp @@ -22,6 +22,12 @@ prebuilt_etc { } prebuilt_etc { + name: "preinstalled-packages-platform.xml", + sub_dir: "sysconfig", + src: "preinstalled-packages-platform.xml", +} + +prebuilt_etc { name: "hiddenapi-package-whitelist.xml", sub_dir: "sysconfig", src: "hiddenapi-package-whitelist.xml", diff --git a/data/etc/platform.xml b/data/etc/platform.xml index 2ab7845a0981..dceb243972e2 100644 --- a/data/etc/platform.xml +++ b/data/etc/platform.xml @@ -229,8 +229,6 @@ <library name="android.hidl.manager-V1.0-java" file="/system/framework/android.hidl.manager-V1.0-java.jar" dependency="android.hidl.base-V1.0-java" /> - <library name="telephony-common" - file="/system/framework/telephony-common.jar" /> <!-- These are the standard packages that are white-listed to always have internet access while in power save mode, even if they aren't in the foreground. --> diff --git a/data/etc/preinstalled-packages-platform.xml b/data/etc/preinstalled-packages-platform.xml new file mode 100644 index 000000000000..ccd8b5bb5347 --- /dev/null +++ b/data/etc/preinstalled-packages-platform.xml @@ -0,0 +1,90 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2019 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. +--> + +<!-- +This XML file declares which system packages should be initially installed for new users based on +the type of user. All system packages on the device should ideally have an entry in an xml file +(keys by its manifest name). + +Main user-types (every user will be at least one of these types) are: + SYSTEM (user 0) + FULL (any non-profile human user) + PROFILE (profile human user) + +Additional optional types are: GUEST, RESTRICTED, MANAGED_PROFILE, EPHEMERAL, DEMO + +The meaning of each of these user types is delineated by flags in +frameworks/base/core/java/android/content/pm/UserInfo.java. +See frameworks/base/services/core/java/com/android/server/pm/UserSystemPackageInstaller#getFlagsFromUserTypes + +The following three examples should cover most normal cases: + +1. For a system package to be pre-installed only in user 0: + + <install-in-user-type package="com.android.example"> + <install-in user-type="SYSTEM"> + </install-in-user-type> + + +2. For a system package to be pre-installed on all human users (e.g. a web browser), i.e. to be +installed on any user of type type FULL or PROFILE (since this covers all human users): + + <install-in-user-type package="com.android.example"> + <install-in user-type="FULL"> + <install-in user-type="PROFILE"> + </install-in-user-type> + + +3. For a system package to be pre-installed on all human users except for profile users (e.g. a +wallpaper app, since profiles cannot display wallpaper): + + <install-in-user-type package="com.android.example"> + <install-in user-type="FULL"> + </install-in-user-type> + + +Some system packages truly are required to be on all users, regardless of type, in which case use: + <install-in-user-type package="com.android.example"> + <install-in user-type="SYSTEM"> + <install-in user-type="FULL"> + <install-in user-type="PROFILE"> + </install-in-user-type> + +More fine-grained options are also available (see below). Additionally, packages can blacklist +user types. Blacklists override any whitelisting (in any file). +E.g. + <install-in-user-type package="com.android.example"> + <install-in user-type="FULL" /> + <do-not-install-in user-type="GUEST" /> + </install-in-user-type> + +If a user is of type FULL and GUEST, this package will NOT be installed, because the +'do-not-install-in' takes precedence over 'install-in'. + +The way that a device treats system packages that do not have any entry (for any user type) at all +is determined by the config resource value config_userTypePackageWhitelistMode. +See frameworks/base/core/res/res/values/config.xml#config_userTypePackageWhitelistMode. + +Changes to the whitelist during system updates can result in installing new system packages +to pre-existing users, but cannot uninstall system packages from pre-existing users. +--> +<config> + <install-in-user-type package="com.android.providers.settings"> + <install-in user-type="SYSTEM" /> + <install-in user-type="FULL" /> + <install-in user-type="PROFILE" /> + </install-in-user-type> +</config> diff --git a/data/etc/services.core.protolog.json b/data/etc/services.core.protolog.json index 261891fb3bbc..f32935fa300f 100644 --- a/data/etc/services.core.protolog.json +++ b/data/etc/services.core.protolog.json @@ -1,10 +1,11 @@ { "version": "1.0.0", "messages": { - "485522692": { + "594230385": { "message": "Test completed successfully: %b %d %o %x %e %g %f %% %s.", "level": "ERROR", - "group": "TEST_GROUP" + "group": "TEST_GROUP", + "at": "com\/android\/server\/wm\/ProtoLogGroup.java:94" } }, "groups": { diff --git a/keystore/java/android/security/keystore/AttestationUtils.java b/keystore/java/android/security/keystore/AttestationUtils.java index bd497c1e4efa..94499ce24ed0 100644 --- a/keystore/java/android/security/keystore/AttestationUtils.java +++ b/keystore/java/android/security/keystore/AttestationUtils.java @@ -74,6 +74,13 @@ public abstract class AttestationUtils { public static final int ID_TYPE_MEID = 3; /** + * Specifies that the device should attest its MEIDs. For use with {@link #attestDeviceIds}. + * + * @see #attestDeviceIds + */ + public static final int USE_INDIVIDUAL_ATTESTATION = 4; + + /** * Creates an array of X509Certificates from the provided KeymasterCertificateChain. * * @hide Only called by the DevicePolicyManager. @@ -196,6 +203,13 @@ public abstract class AttestationUtils { meid.getBytes(StandardCharsets.UTF_8)); break; } + case USE_INDIVIDUAL_ATTESTATION: { + //TODO: Add the Keymaster tag for requesting the use of individual + //attestation certificate, which should be + //KeymasterDefs.KM_TAG_DEVICE_UNIQUE_ATTESTATION + attestArgs.addBoolean(720); + break; + } default: throw new IllegalArgumentException("Unknown device ID type " + idType); } diff --git a/libs/androidfw/AssetManager2.cpp b/libs/androidfw/AssetManager2.cpp index 01caf011f644..eec49df79630 100644 --- a/libs/androidfw/AssetManager2.cpp +++ b/libs/androidfw/AssetManager2.cpp @@ -224,6 +224,62 @@ const std::unordered_map<std::string, std::string>* return &loaded_package->GetOverlayableMap(); } +bool AssetManager2::GetOverlayablesToString(const android::StringPiece& package_name, + std::string* out) const { + uint8_t package_id = 0U; + for (const auto& apk_assets : apk_assets_) { + const LoadedArsc* loaded_arsc = apk_assets->GetLoadedArsc(); + if (loaded_arsc == nullptr) { + continue; + } + + const auto& loaded_packages = loaded_arsc->GetPackages(); + if (loaded_packages.empty()) { + continue; + } + + const auto& loaded_package = loaded_packages[0]; + if (loaded_package->GetPackageName() == package_name) { + package_id = GetAssignedPackageId(loaded_package.get()); + break; + } + } + + if (package_id == 0U) { + ANDROID_LOG(ERROR) << base::StringPrintf("No package with name '%s", package_name.data()); + return false; + } + + const size_t idx = package_ids_[package_id]; + if (idx == 0xff) { + return false; + } + + std::string output; + for (const ConfiguredPackage& package : package_groups_[idx].packages_) { + const LoadedPackage* loaded_package = package.loaded_package_; + for (auto it = loaded_package->begin(); it != loaded_package->end(); it++) { + const OverlayableInfo* info = loaded_package->GetOverlayableInfo(*it); + if (info != nullptr) { + ResourceName res_name; + if (!GetResourceName(*it, &res_name)) { + ANDROID_LOG(ERROR) << base::StringPrintf( + "Unable to retrieve name of overlayable resource 0x%08x", *it); + return false; + } + + const std::string name = ToFormattedResourceString(&res_name); + output.append(base::StringPrintf( + "resource='%s' overlayable='%s' actor='%s' policy='0x%08x'\n", + name.c_str(), info->name.c_str(), info->actor.c_str(), info->policy_flags)); + } + } + } + + *out = std::move(output); + return true; +} + void AssetManager2::SetConfiguration(const ResTable_config& configuration) { const int diff = configuration_.diff(configuration); configuration_ = configuration; @@ -1073,7 +1129,7 @@ void AssetManager2::InvalidateCaches(uint32_t diff) { } } -uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) { +uint8_t AssetManager2::GetAssignedPackageId(const LoadedPackage* package) const { for (auto& package_group : package_groups_) { for (auto& package2 : package_group.packages_) { if (package2.loaded_package_ == package) { diff --git a/libs/androidfw/include/androidfw/AssetManager2.h b/libs/androidfw/include/androidfw/AssetManager2.h index 1e2b36cb1703..de46081a6aa3 100644 --- a/libs/androidfw/include/androidfw/AssetManager2.h +++ b/libs/androidfw/include/androidfw/AssetManager2.h @@ -124,6 +124,10 @@ class AssetManager2 { // This may be nullptr if the APK represented by `cookie` has no resource table. const DynamicRefTable* GetDynamicRefTableForCookie(ApkAssetsCookie cookie) const; + // Returns a string representation of the overlayable API of a package. + bool GetOverlayablesToString(const android::StringPiece& package_name, + std::string* out) const; + const std::unordered_map<std::string, std::string>* GetOverlayableMapForPackage(uint32_t package_id) const; @@ -308,7 +312,7 @@ class AssetManager2 { const ResolvedBag* GetBag(uint32_t resid, std::vector<uint32_t>& child_resids); // Retrieve the assigned package id of the package if loaded into this AssetManager - uint8_t GetAssignedPackageId(const LoadedPackage* package); + uint8_t GetAssignedPackageId(const LoadedPackage* package) const; // The ordered list of ApkAssets to search. These are not owned by the AssetManager, and must // have a longer lifetime. diff --git a/libs/androidfw/tests/AssetManager2_test.cpp b/libs/androidfw/tests/AssetManager2_test.cpp index 40c8e46e4d84..15910241518d 100644 --- a/libs/androidfw/tests/AssetManager2_test.cpp +++ b/libs/androidfw/tests/AssetManager2_test.cpp @@ -707,7 +707,7 @@ TEST_F(AssetManager2Test, GetLastPathAfterDisablingReturnsEmpty) { EXPECT_EQ("", resultDisabled); } -TEST_F(AssetManager2Test, GetOverlayableMap) { +TEST_F(AssetManager2Test, GetOverlayablesToString) { ResTable_config desired_config; memset(&desired_config, 0, sizeof(desired_config)); @@ -721,6 +721,12 @@ TEST_F(AssetManager2Test, GetOverlayableMap) { ASSERT_EQ(2, map->size()); ASSERT_EQ(map->at("OverlayableResources1"), "overlay://theme"); ASSERT_EQ(map->at("OverlayableResources2"), "overlay://com.android.overlayable"); + + std::string api; + ASSERT_TRUE(assetmanager.GetOverlayablesToString("com.android.overlayable", &api)); + ASSERT_EQ(api.find("not_overlayable"), std::string::npos); + ASSERT_NE(api.find("resource='com.android.overlayable:string/overlayable2' overlayable='OverlayableResources1' actor='overlay://theme' policy='0x0000000a'\n"), + std::string::npos); } } // namespace android diff --git a/location/java/android/location/AbstractListenerManager.java b/location/java/android/location/AbstractListenerManager.java new file mode 100644 index 000000000000..c41023e31065 --- /dev/null +++ b/location/java/android/location/AbstractListenerManager.java @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2014 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 android.location; + +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.os.Binder; +import android.os.Handler; +import android.os.HandlerExecutor; +import android.os.RemoteException; +import android.util.ArrayMap; + +import com.android.internal.annotations.GuardedBy; +import com.android.internal.util.Preconditions; + +import java.util.concurrent.Executor; +import java.util.function.Consumer; + +/** + * A base class to manage listeners that have a 1:N -> source:listener relationship. + * + * @hide + */ +abstract class AbstractListenerManager<T> { + + private static class Registration<T> { + private final Executor mExecutor; + @Nullable private volatile T mListener; + + private Registration(Executor executor, T listener) { + Preconditions.checkArgument(listener != null); + Preconditions.checkArgument(executor != null); + mExecutor = executor; + mListener = listener; + } + + private void unregister() { + mListener = null; + } + + private void execute(Consumer<T> operation) { + mExecutor.execute(() -> { + T listener = mListener; + if (listener == null) { + return; + } + + // we may be under the binder identity if a direct executor is used + long identity = Binder.clearCallingIdentity(); + try { + operation.accept(listener); + } finally { + Binder.restoreCallingIdentity(identity); + } + }); + } + } + + @GuardedBy("mListeners") + private final ArrayMap<Object, Registration<T>> mListeners = new ArrayMap<>(); + + public boolean addListener(@NonNull T listener, @NonNull Handler handler) + throws RemoteException { + return addInternal(listener, handler); + } + + public boolean addListener(@NonNull T listener, @NonNull Executor executor) + throws RemoteException { + return addInternal(listener, executor); + } + + protected final boolean addInternal(Object listener, Handler handler) throws RemoteException { + return addInternal(listener, new HandlerExecutor(handler)); + } + + protected final boolean addInternal(Object listener, Executor executor) throws RemoteException { + return addInternal(listener, new Registration<>(executor, convertKey(listener))); + } + + private boolean addInternal(Object key, Registration<T> registration) throws RemoteException { + Preconditions.checkNotNull(key); + Preconditions.checkNotNull(registration); + + synchronized (mListeners) { + if (mListeners.isEmpty() && !registerService()) { + return false; + } + Registration<T> oldRegistration = mListeners.put(key, registration); + if (oldRegistration != null) { + oldRegistration.unregister(); + } + return true; + } + } + + public void removeListener(Object listener) throws RemoteException { + synchronized (mListeners) { + Registration<T> oldRegistration = mListeners.remove(listener); + if (oldRegistration == null) { + return; + } + oldRegistration.unregister(); + + if (mListeners.isEmpty()) { + unregisterService(); + } + } + } + + @SuppressWarnings("unchecked") + protected T convertKey(@NonNull Object listener) { + return (T) listener; + } + + protected abstract boolean registerService() throws RemoteException; + protected abstract void unregisterService() throws RemoteException; + + protected void execute(Consumer<T> operation) { + synchronized (mListeners) { + for (Registration<T> registration : mListeners.values()) { + registration.execute(operation); + } + } + } +} diff --git a/location/java/android/location/BatchedLocationCallbackTransport.java b/location/java/android/location/BatchedLocationCallbackTransport.java deleted file mode 100644 index e00f855e9302..000000000000 --- a/location/java/android/location/BatchedLocationCallbackTransport.java +++ /dev/null @@ -1,66 +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 android.location; - -import android.content.Context; -import android.os.RemoteException; - -import java.util.List; - -/** - * A handler class to manage transport callbacks for {@link BatchedLocationCallback}. - * - * @hide - */ -class BatchedLocationCallbackTransport - extends LocalListenerHelper<BatchedLocationCallback> { - private final ILocationManager mLocationManager; - - private final IBatchedLocationCallback mCallbackTransport = new CallbackTransport(); - - public BatchedLocationCallbackTransport(Context context, ILocationManager locationManager) { - super(context, "BatchedLocationCallbackTransport"); - mLocationManager = locationManager; - } - - @Override - protected boolean registerWithServer() throws RemoteException { - return mLocationManager.addGnssBatchingCallback( - mCallbackTransport, - getContext().getPackageName()); - } - - @Override - protected void unregisterFromServer() throws RemoteException { - mLocationManager.removeGnssBatchingCallback(); - } - - private class CallbackTransport extends IBatchedLocationCallback.Stub { - @Override - public void onLocationBatch(final List<Location> locations) { - ListenerOperation<BatchedLocationCallback> operation = - new ListenerOperation<BatchedLocationCallback>() { - @Override - public void execute(BatchedLocationCallback callback) - throws RemoteException { - callback.onLocationBatch(locations); - } - }; - foreach(operation); - } - } -} diff --git a/location/java/android/location/GnssMeasurementCallbackTransport.java b/location/java/android/location/GnssMeasurementCallbackTransport.java deleted file mode 100644 index 8cb8c0b78da1..000000000000 --- a/location/java/android/location/GnssMeasurementCallbackTransport.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2014 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 android.location; - -import android.content.Context; -import android.os.RemoteException; - -import com.android.internal.util.Preconditions; - -/** - * A handler class to manage transport callbacks for {@link GnssMeasurementsEvent.Callback}. - * - * @hide - */ -class GnssMeasurementCallbackTransport - extends LocalListenerHelper<GnssMeasurementsEvent.Callback> { - private static final String TAG = "GnssMeasCbTransport"; - private final ILocationManager mLocationManager; - - private final IGnssMeasurementsListener mListenerTransport = new ListenerTransport(); - - public GnssMeasurementCallbackTransport(Context context, ILocationManager locationManager) { - super(context, TAG); - mLocationManager = locationManager; - } - - @Override - protected boolean registerWithServer() throws RemoteException { - return mLocationManager.addGnssMeasurementsListener( - mListenerTransport, - getContext().getPackageName()); - } - - @Override - protected void unregisterFromServer() throws RemoteException { - mLocationManager.removeGnssMeasurementsListener(mListenerTransport); - } - - /** - * Injects GNSS measurement corrections into the GNSS chipset. - * - * @param measurementCorrections a {@link GnssMeasurementCorrections} object with the GNSS - * measurement corrections to be injected into the GNSS chipset. - */ - protected void injectGnssMeasurementCorrections( - GnssMeasurementCorrections measurementCorrections) throws RemoteException { - Preconditions.checkNotNull(measurementCorrections); - mLocationManager.injectGnssMeasurementCorrections( - measurementCorrections, getContext().getPackageName()); - } - - protected long getGnssCapabilities() throws RemoteException { - return mLocationManager.getGnssCapabilities(getContext().getPackageName()); - } - - private class ListenerTransport extends IGnssMeasurementsListener.Stub { - @Override - public void onGnssMeasurementsReceived(final GnssMeasurementsEvent event) { - ListenerOperation<GnssMeasurementsEvent.Callback> operation = - new ListenerOperation<GnssMeasurementsEvent.Callback>() { - @Override - public void execute(GnssMeasurementsEvent.Callback callback) - throws RemoteException { - callback.onGnssMeasurementsReceived(event); - } - }; - foreach(operation); - } - - @Override - public void onStatusChanged(final int status) { - ListenerOperation<GnssMeasurementsEvent.Callback> operation = - new ListenerOperation<GnssMeasurementsEvent.Callback>() { - @Override - public void execute(GnssMeasurementsEvent.Callback callback) - throws RemoteException { - callback.onStatusChanged(status); - } - }; - foreach(operation); - } - } -} diff --git a/location/java/android/location/GnssNavigationMessageCallbackTransport.java b/location/java/android/location/GnssNavigationMessageCallbackTransport.java deleted file mode 100644 index 1eafd02e52be..000000000000 --- a/location/java/android/location/GnssNavigationMessageCallbackTransport.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2014 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 android.location; - -import android.content.Context; -import android.os.RemoteException; - -/** - * A handler class to manage transport callback for {@link GnssNavigationMessage.Callback}. - * - * @hide - */ -class GnssNavigationMessageCallbackTransport - extends LocalListenerHelper<GnssNavigationMessage.Callback> { - private final ILocationManager mLocationManager; - - private final IGnssNavigationMessageListener mListenerTransport = new ListenerTransport(); - - public GnssNavigationMessageCallbackTransport( - Context context, - ILocationManager locationManager) { - super(context, "GnssNavigationMessageCallbackTransport"); - mLocationManager = locationManager; - } - - @Override - protected boolean registerWithServer() throws RemoteException { - return mLocationManager.addGnssNavigationMessageListener( - mListenerTransport, - getContext().getPackageName()); - } - - @Override - protected void unregisterFromServer() throws RemoteException { - mLocationManager.removeGnssNavigationMessageListener(mListenerTransport); - } - - private class ListenerTransport extends IGnssNavigationMessageListener.Stub { - @Override - public void onGnssNavigationMessageReceived(final GnssNavigationMessage event) { - ListenerOperation<GnssNavigationMessage.Callback> operation = - new ListenerOperation<GnssNavigationMessage.Callback>() { - @Override - public void execute(GnssNavigationMessage.Callback callback) - throws RemoteException { - callback.onGnssNavigationMessageReceived(event); - } - }; - foreach(operation); - } - - @Override - public void onStatusChanged(final int status) { - ListenerOperation<GnssNavigationMessage.Callback> operation = - new ListenerOperation<GnssNavigationMessage.Callback>() { - @Override - public void execute(GnssNavigationMessage.Callback callback) - throws RemoteException { - callback.onStatusChanged(status); - } - }; - foreach(operation); - } - } -} diff --git a/location/java/android/location/LocalListenerHelper.java b/location/java/android/location/LocalListenerHelper.java deleted file mode 100644 index 592d01d2fed6..000000000000 --- a/location/java/android/location/LocalListenerHelper.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * Copyright (C) 2014 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 android.location; - -import android.annotation.NonNull; -import android.content.Context; -import android.os.Handler; -import android.os.RemoteException; -import android.util.Log; - -import com.android.internal.util.Preconditions; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -/** - * A base handler class to manage transport and local listeners. - * - * @hide - */ -abstract class LocalListenerHelper<TListener> { - private final HashMap<TListener, Handler> mListeners = new HashMap<>(); - - private final String mTag; - private final Context mContext; - - protected LocalListenerHelper(Context context, String name) { - Preconditions.checkNotNull(name); - mContext = context; - mTag = name; - } - - /** - * Adds a {@param listener} to the list of listeners on which callbacks will be executed. The - * execution will happen on the {@param handler} thread or alternatively in the callback thread - * if a {@code null} handler value is passed. - */ - public boolean add(@NonNull TListener listener, Handler handler) { - Preconditions.checkNotNull(listener); - synchronized (mListeners) { - // we need to register with the service first, because we need to find out if the - // service will actually support the request before we attempt anything - if (mListeners.isEmpty()) { - boolean registeredWithService; - try { - registeredWithService = registerWithServer(); - } catch (RemoteException e) { - Log.e(mTag, "Error handling first listener.", e); - return false; - } - if (!registeredWithService) { - Log.e(mTag, "Unable to register listener transport."); - return false; - } - } - if (mListeners.containsKey(listener)) { - return true; - } - mListeners.put(listener, handler); - return true; - } - } - - public void remove(@NonNull TListener listener) { - Preconditions.checkNotNull(listener); - synchronized (mListeners) { - boolean removed = mListeners.containsKey(listener); - mListeners.remove(listener); - boolean isLastRemoved = removed && mListeners.isEmpty(); - if (isLastRemoved) { - try { - unregisterFromServer(); - } catch (RemoteException e) { - Log.v(mTag, "Error handling last listener removal", e); - } - } - } - } - - protected abstract boolean registerWithServer() throws RemoteException; - protected abstract void unregisterFromServer() throws RemoteException; - - protected interface ListenerOperation<TListener> { - void execute(TListener listener) throws RemoteException; - } - - protected Context getContext() { - return mContext; - } - - private void executeOperation(ListenerOperation<TListener> operation, TListener listener) { - try { - operation.execute(listener); - } catch (RemoteException e) { - Log.e(mTag, "Error in monitored listener.", e); - // don't return, give a fair chance to all listeners to receive the event - } - } - - protected void foreach(final ListenerOperation<TListener> operation) { - Collection<Map.Entry<TListener, Handler>> listeners; - synchronized (mListeners) { - listeners = new ArrayList<>(mListeners.entrySet()); - } - for (final Map.Entry<TListener, Handler> listener : listeners) { - if (listener.getValue() == null) { - executeOperation(operation, listener.getKey()); - } else { - listener.getValue().post(new Runnable() { - @Override - public void run() { - executeOperation(operation, listener.getKey()); - } - }); - } - } - } -} diff --git a/location/java/android/location/LocationManager.java b/location/java/android/location/LocationManager.java index 5be4770440dc..6b47d1dff781 100644 --- a/location/java/android/location/LocationManager.java +++ b/location/java/android/location/LocationManager.java @@ -22,6 +22,7 @@ import static android.Manifest.permission.LOCATION_HARDWARE; import static android.Manifest.permission.WRITE_SECURE_SETTINGS; import android.Manifest; +import android.annotation.CallbackExecutor; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresFeature; @@ -33,13 +34,13 @@ import android.annotation.TestApi; import android.annotation.UnsupportedAppUsage; import android.app.PendingIntent; import android.content.Context; -import android.content.Intent; import android.content.pm.PackageManager; +import android.os.Binder; import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.os.HandlerExecutor; import android.os.Looper; -import android.os.Message; import android.os.Process; import android.os.RemoteException; import android.os.UserHandle; @@ -47,47 +48,32 @@ import android.provider.Settings; import android.util.ArrayMap; import android.util.Log; +import com.android.internal.annotations.GuardedBy; import com.android.internal.location.ProviderProperties; import com.android.internal.util.Preconditions; -import java.util.ArrayList; import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.RejectedExecutionException; /** - * This class provides access to the system location services. These - * services allow applications to obtain periodic updates of the - * device's geographical location, or to fire an application-specified - * {@link Intent} when the device enters the proximity of a given - * geographical location. + * This class provides access to the system location services. These services allow applications to + * obtain periodic updates of the device's geographical location, or to be notified when the device + * enters the proximity of a given geographical location. * - * <p class="note">Unless noted, all Location API methods require - * the {@link android.Manifest.permission#ACCESS_COARSE_LOCATION} or - * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permissions. - * If your application only has the coarse permission then it will not have - * access to the GPS or passive location providers. Other providers will still - * return location results, but the update rate will be throttled and the exact - * location will be obfuscated to a coarse level of accuracy. + * <p class="note">Unless noted, all Location API methods require the {@link + * android.Manifest.permission#ACCESS_COARSE_LOCATION} or {@link + * android.Manifest.permission#ACCESS_FINE_LOCATION} permissions. If your application only has the + * coarse permission then it will not have access to fine location providers. Other providers will + * still return location results, but the exact location will be obfuscated to a coarse level of + * accuracy. */ +@SuppressWarnings({"deprecation", "DeprecatedIsStillUsed"}) @SystemService(Context.LOCATION_SERVICE) @RequiresFeature(PackageManager.FEATURE_LOCATION) public class LocationManager { - private static final String TAG = "LocationManager"; - private final Context mContext; - @UnsupportedAppUsage - private final ILocationManager mService; - private final GnssMeasurementCallbackTransport mGnssMeasurementCallbackTransport; - private final GnssNavigationMessageCallbackTransport mGnssNavigationMessageCallbackTransport; - private final BatchedLocationCallbackTransport mBatchedLocationCallbackTransport; - private final ArrayMap<GnssStatus.Callback, GnssStatusListenerTransport> mGnssStatusListeners = - new ArrayMap<>(); - private final ArrayMap<OnNmeaMessageListener, GnssStatusListenerTransport> mGnssNmeaListeners = - new ArrayMap<>(); - private final ArrayMap<GpsStatus.Listener, GnssStatusListenerTransport> mGpsStatusListeners = - new ArrayMap<>(); - // volatile + GnssStatus final-fields pattern to avoid a partially published object - private volatile GnssStatus mGnssStatus; - private int mTimeToFirstFix; + private static final String TAG = "LocationManager"; /** * Name of the network location provider. @@ -238,262 +224,440 @@ public class LocationManager { public static final String METADATA_SETTINGS_FOOTER_STRING = "com.android.settings.location.FOOTER_STRING"; - // Map from LocationListeners to their associated ListenerTransport objects - private final ArrayMap<LocationListener, ListenerTransport> mListeners = new ArrayMap<>(); + private final Context mContext; - private class ListenerTransport extends ILocationListener.Stub { - private static final int TYPE_LOCATION_CHANGED = 1; - private static final int TYPE_STATUS_CHANGED = 2; - private static final int TYPE_PROVIDER_ENABLED = 3; - private static final int TYPE_PROVIDER_DISABLED = 4; + @UnsupportedAppUsage + private final ILocationManager mService; - private LocationListener mListener; - private final Handler mListenerHandler; + @GuardedBy("mListeners") + private final ArrayMap<LocationListener, LocationListenerTransport> mListeners = + new ArrayMap<>(); - ListenerTransport(LocationListener listener, Looper looper) { - mListener = listener; + @GuardedBy("mBatchedLocationCallbackManager") + private final BatchedLocationCallbackManager mBatchedLocationCallbackManager = + new BatchedLocationCallbackManager(); + private final GnssStatusListenerManager + mGnssStatusListenerManager = new GnssStatusListenerManager(); + private final GnssMeasurementsListenerManager mGnssMeasurementsListenerManager = + new GnssMeasurementsListenerManager(); + private final GnssNavigationMessageListenerManager mGnssNavigationMessageListenerTransport = + new GnssNavigationMessageListenerManager(); - if (looper == null) { - mListenerHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - _handleMessage(msg); - } - }; - } else { - mListenerHandler = new Handler(looper) { - @Override - public void handleMessage(Message msg) { - _handleMessage(msg); - } - }; - } - } + /** + * @hide + */ + public LocationManager(@NonNull Context context, @NonNull ILocationManager service) { + mService = service; + mContext = context; + } - @Override - public void onLocationChanged(Location location) { - Message msg = Message.obtain(); - msg.what = TYPE_LOCATION_CHANGED; - msg.obj = location; - sendCallbackMessage(msg); + /** + * @hide + */ + @TestApi + public @NonNull String[] getBackgroundThrottlingWhitelist() { + try { + return mService.getBackgroundThrottlingWhitelist(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } + } - @Override - public void onStatusChanged(String provider, int status, Bundle extras) { - Message msg = Message.obtain(); - msg.what = TYPE_STATUS_CHANGED; - Bundle b = new Bundle(); - b.putString("provider", provider); - b.putInt("status", status); - if (extras != null) { - b.putBundle("extras", extras); - } - msg.obj = b; - sendCallbackMessage(msg); + /** + * @hide + */ + @TestApi + public @NonNull String[] getIgnoreSettingsWhitelist() { + try { + return mService.getIgnoreSettingsWhitelist(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } + } - @Override - public void onProviderEnabled(String provider) { - Message msg = Message.obtain(); - msg.what = TYPE_PROVIDER_ENABLED; - msg.obj = provider; - sendCallbackMessage(msg); + /** + * Returns the extra location controller package on the device. + * + * @hide + */ + @SystemApi + public @Nullable String getExtraLocationControllerPackage() { + try { + return mService.getExtraLocationControllerPackage(); + } catch (RemoteException e) { + e.rethrowFromSystemServer(); + return null; } + } - @Override - public void onProviderDisabled(String provider) { - Message msg = Message.obtain(); - msg.what = TYPE_PROVIDER_DISABLED; - msg.obj = provider; - sendCallbackMessage(msg); + /** + * Set the extra location controller package for location services on the device. + * + * @hide + */ + @SystemApi + @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) + public void setExtraLocationControllerPackage(@Nullable String packageName) { + try { + mService.setExtraLocationControllerPackage(packageName); + } catch (RemoteException e) { + e.rethrowFromSystemServer(); } + } - private void sendCallbackMessage(Message msg) { - if (!mListenerHandler.sendMessage(msg)) { - locationCallbackFinished(); - } + /** + * Set whether the extra location controller package is currently enabled on the device. + * + * @hide + */ + @SystemApi + @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) + public void setExtraLocationControllerPackageEnabled(boolean enabled) { + try { + mService.setExtraLocationControllerPackageEnabled(enabled); + } catch (RemoteException e) { + e.rethrowFromSystemServer(); } + } - private void _handleMessage(Message msg) { - switch (msg.what) { - case TYPE_LOCATION_CHANGED: - Location location = new Location((Location) msg.obj); - mListener.onLocationChanged(location); - break; - case TYPE_STATUS_CHANGED: - Bundle b = (Bundle) msg.obj; - String provider = b.getString("provider"); - int status = b.getInt("status"); - Bundle extras = b.getBundle("extras"); - mListener.onStatusChanged(provider, status, extras); - break; - case TYPE_PROVIDER_ENABLED: - mListener.onProviderEnabled((String) msg.obj); - break; - case TYPE_PROVIDER_DISABLED: - mListener.onProviderDisabled((String) msg.obj); - break; - } - locationCallbackFinished(); + /** + * Returns whether extra location controller package is currently enabled on the device. + * + * @hide + */ + @SystemApi + public boolean isExtraLocationControllerPackageEnabled() { + try { + return mService.isExtraLocationControllerPackageEnabled(); + } catch (RemoteException e) { + e.rethrowFromSystemServer(); + return false; } + } - private void locationCallbackFinished() { - try { - mService.locationCallbackFinished(this); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + /** + * Set the extra location controller package for location services on the device. + * + * @removed + * @deprecated Use {@link #setExtraLocationControllerPackage} instead. + * @hide + */ + @Deprecated + @SystemApi + @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) + public void setLocationControllerExtraPackage(String packageName) { + try { + mService.setExtraLocationControllerPackage(packageName); + } catch (RemoteException e) { + e.rethrowFromSystemServer(); } } /** + * Set whether the extra location controller package is currently enabled on the device. + * + * @removed + * @deprecated Use {@link #setExtraLocationControllerPackageEnabled} instead. * @hide */ - @TestApi - public @NonNull String[] getBackgroundThrottlingWhitelist() { + @SystemApi + @Deprecated + @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) + public void setLocationControllerExtraPackageEnabled(boolean enabled) { try { - return mService.getBackgroundThrottlingWhitelist(); + mService.setExtraLocationControllerPackageEnabled(enabled); } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); + e.rethrowFromSystemServer(); } } /** + * Returns the current enabled/disabled state of location. To listen for changes, see + * {@link #MODE_CHANGED_ACTION}. + * + * @return true if location is enabled and false if location is disabled. + */ + public boolean isLocationEnabled() { + return isLocationEnabledForUser(Process.myUserHandle()); + } + + /** + * Returns the current enabled/disabled state of location. + * + * @param userHandle the user to query + * @return true if location is enabled and false if location is disabled. + * * @hide */ - @TestApi - public @NonNull String[] getIgnoreSettingsWhitelist() { + @SystemApi + public boolean isLocationEnabledForUser(@NonNull UserHandle userHandle) { try { - return mService.getIgnoreSettingsWhitelist(); + return mService.isLocationEnabledForUser(userHandle.getIdentifier()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * @hide - hide this constructor because it has a parameter - * of type ILocationManager, which is a system private class. The - * right way to create an instance of this class is using the - * factory Context.getSystemService. + * Enables or disables the location setting. + * + * @param enabled true to enable location and false to disable location. + * @param userHandle the user to set + * + * @hide */ - public LocationManager(@NonNull Context context, @NonNull ILocationManager service) { - mService = service; - mContext = context; - mGnssMeasurementCallbackTransport = - new GnssMeasurementCallbackTransport(mContext, mService); - mGnssNavigationMessageCallbackTransport = - new GnssNavigationMessageCallbackTransport(mContext, mService); - mBatchedLocationCallbackTransport = - new BatchedLocationCallbackTransport(mContext, mService); - + @SystemApi + @TestApi + @RequiresPermission(WRITE_SECURE_SETTINGS) + public void setLocationEnabledForUser(boolean enabled, @NonNull UserHandle userHandle) { + Settings.Secure.putIntForUser( + mContext.getContentResolver(), + Settings.Secure.LOCATION_MODE, + enabled + ? Settings.Secure.LOCATION_MODE_ON + : Settings.Secure.LOCATION_MODE_OFF, + userHandle.getIdentifier()); } - private LocationProvider createProvider(String name, ProviderProperties properties) { - return new LocationProvider(name, properties); + /** + * Returns the current enabled/disabled status of the given provider. To listen for changes, see + * {@link #PROVIDERS_CHANGED_ACTION}. + * + * Before API version {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method would throw + * {@link SecurityException} if the location permissions were not sufficient to use the + * specified provider. + * + * @param provider the name of the provider + * @return true if the provider exists and is enabled + * + * @throws IllegalArgumentException if provider is null + */ + public boolean isProviderEnabled(@NonNull String provider) { + return isProviderEnabledForUser(provider, Process.myUserHandle()); } /** - * Returns a list of the names of all known location providers. - * <p>All providers are returned, including ones that are not permitted to - * be accessed by the calling activity or are currently disabled. + * Returns the current enabled/disabled status of the given provider and user. Callers should + * prefer {@link #isLocationEnabledForUser(UserHandle)} unless they depend on provider-specific + * APIs. * - * @return list of Strings containing names of the provider + * Before API version {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method would throw + * {@link SecurityException} if the location permissions were not sufficient to use the + * specified provider. + * + * @param provider the name of the provider + * @param userHandle the user to query + * @return true if the provider exists and is enabled + * + * @throws IllegalArgumentException if provider is null + * @hide */ - public @NonNull List<String> getAllProviders() { + @SystemApi + public boolean isProviderEnabledForUser( + @NonNull String provider, @NonNull UserHandle userHandle) { + checkProvider(provider); + try { - return mService.getAllProviders(); + return mService.isProviderEnabledForUser(provider, userHandle.getIdentifier()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Returns a list of the names of location providers. + * Method for enabling or disabling a single location provider. This method is deprecated and + * functions as a best effort. It should not be relied on in any meaningful sense as providers + * may no longer be enabled or disabled by clients. * - * @param enabledOnly if true then only the providers which are currently - * enabled are returned. - * @return list of Strings containing names of the providers + * @param provider the name of the provider + * @param enabled true to enable the provider. false to disable the provider + * @param userHandle the user to set + * @return true if the value was set, false otherwise + * + * @throws IllegalArgumentException if provider is null + * @deprecated Do not manipulate providers individually, use + * {@link #setLocationEnabledForUser(boolean, UserHandle)} instead. + * @hide */ - public @NonNull List<String> getProviders(boolean enabledOnly) { + @Deprecated + @SystemApi + @RequiresPermission(WRITE_SECURE_SETTINGS) + public boolean setProviderEnabledForUser( + @NonNull String provider, boolean enabled, @NonNull UserHandle userHandle) { + checkProvider(provider); + + return Settings.Secure.putStringForUser( + mContext.getContentResolver(), + Settings.Secure.LOCATION_PROVIDERS_ALLOWED, + (enabled ? "+" : "-") + provider, + userHandle.getIdentifier()); + } + + /** + * Get the last known location. + * + * <p>This location could be very old so use + * {@link Location#getElapsedRealtimeNanos} to calculate its age. It can + * also return null if no previous location is available. + * + * <p>Always returns immediately. + * + * @return The last known location, or null if not available + * @throws SecurityException if no suitable permission is present + * + * @hide + */ + @Nullable + public Location getLastLocation() { + String packageName = mContext.getPackageName(); + try { - return mService.getProviders(null, enabledOnly); + return mService.getLastLocation(null, packageName); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Returns the information associated with the location provider of the - * given name, or null if no provider exists by that name. + * Returns a Location indicating the data from the last known + * location fix obtained from the given provider. * - * @param name the provider name - * @return a LocationProvider, or null + * <p> This can be done + * without starting the provider. Note that this location could + * be out-of-date, for example if the device was turned off and + * moved to another location. * - * @throws IllegalArgumentException if name is null or does not exist - * @throws SecurityException if the caller is not permitted to access the - * given provider. + * <p> If the provider is currently disabled, null is returned. + * + * @param provider the name of the provider + * @return the last known location for the provider, or null + * + * @throws SecurityException if no suitable permission is present + * @throws IllegalArgumentException if provider is null or doesn't exist */ - public @Nullable LocationProvider getProvider(@NonNull String name) { - checkProvider(name); + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + @Nullable + public Location getLastKnownLocation(@NonNull String provider) { + checkProvider(provider); + String packageName = mContext.getPackageName(); + LocationRequest request = LocationRequest.createFromDeprecatedProvider( + provider, 0, 0, true); + try { - ProviderProperties properties = mService.getProviderProperties(name); - if (properties == null) { - return null; - } - return createProvider(name, properties); + return mService.getLastLocation(request, packageName); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Returns a list of the names of LocationProviders that satisfy the given - * criteria, or null if none do. Only providers that are permitted to be - * accessed by the calling activity will be returned. + * Register for a single location update using the named provider and + * a callback. * - * @param criteria the criteria that the returned providers must match - * @param enabledOnly if true then only the providers which are currently - * enabled are returned. - * @return list of Strings containing names of the providers + * <p>See {@link #requestLocationUpdates(long, float, Criteria, PendingIntent)} + * for more detail on how to use this method. + * + * @param provider the name of the provider with which to register + * @param listener a {@link LocationListener} whose + * {@link LocationListener#onLocationChanged} method will be called when + * the location update is available + * @param looper a Looper object whose message queue will be used to + * implement the callback mechanism, or null to make callbacks on the calling + * thread + * + * @throws IllegalArgumentException if provider is null or doesn't exist + * @throws IllegalArgumentException if listener is null + * @throws SecurityException if no suitable permission is present */ - public @NonNull List<String> getProviders(@NonNull Criteria criteria, boolean enabledOnly) { + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + public void requestSingleUpdate( + @NonNull String provider, @NonNull LocationListener listener, @Nullable Looper looper) { + checkProvider(provider); + checkListener(listener); + + LocationRequest request = LocationRequest.createFromDeprecatedProvider( + provider, 0, 0, true); + requestLocationUpdates(request, listener, looper); + } + + /** + * Register for a single location update using a Criteria and + * a callback. + * + * <p>See {@link #requestLocationUpdates(long, float, Criteria, PendingIntent)} + * for more detail on how to use this method. + * + * @param criteria contains parameters for the location manager to choose the + * appropriate provider and parameters to compute the location + * @param listener a {@link LocationListener} whose + * {@link LocationListener#onLocationChanged} method will be called when + * the location update is available + * @param looper a Looper object whose message queue will be used to + * implement the callback mechanism, or null to make callbacks on the calling + * thread + * + * @throws IllegalArgumentException if criteria is null + * @throws IllegalArgumentException if listener is null + * @throws SecurityException if no suitable permission is present + */ + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + public void requestSingleUpdate( + @NonNull Criteria criteria, + @NonNull LocationListener listener, + @Nullable Looper looper) { checkCriteria(criteria); - try { - return mService.getProviders(criteria, enabledOnly); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + checkListener(listener); + + LocationRequest request = LocationRequest.createFromDeprecatedCriteria( + criteria, 0, 0, true); + requestLocationUpdates(request, listener, looper); } /** - * Returns the name of the provider that best meets the given criteria. Only providers - * that are permitted to be accessed by the calling activity will be - * returned. If several providers meet the criteria, the one with the best - * accuracy is returned. If no provider meets the criteria, - * the criteria are loosened in the following sequence: + * Register for a single location update using a named provider and pending intent. * - * <ul> - * <li> power requirement - * <li> accuracy - * <li> bearing - * <li> speed - * <li> altitude - * </ul> + * <p>See {@link #requestLocationUpdates(long, float, Criteria, PendingIntent)} + * for more detail on how to use this method. * - * <p> Note that the requirement on monetary cost is not removed - * in this process. + * @param provider the name of the provider with which to register + * @param intent a {@link PendingIntent} to be sent for the location update * - * @param criteria the criteria that need to be matched - * @param enabledOnly if true then only a provider that is currently enabled is returned - * @return name of the provider that best matches the requirements + * @throws IllegalArgumentException if provider is null or doesn't exist + * @throws IllegalArgumentException if intent is null + * @throws SecurityException if no suitable permission is present */ - public @Nullable String getBestProvider(@NonNull Criteria criteria, boolean enabledOnly) { + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + public void requestSingleUpdate(@NonNull String provider, @NonNull PendingIntent intent) { + checkProvider(provider); + checkPendingIntent(intent); + + LocationRequest request = LocationRequest.createFromDeprecatedProvider( + provider, 0, 0, true); + requestLocationUpdates(request, intent); + } + + /** + * Register for a single location update using a Criteria and pending intent. + * + * <p>See {@link #requestLocationUpdates(long, float, Criteria, PendingIntent)} + * for more detail on how to use this method. + * + * @param criteria contains parameters for the location manager to choose the + * appropriate provider and parameters to compute the location + * @param intent a {@link PendingIntent} to be sent for the location update + * + * @throws IllegalArgumentException if provider is null or doesn't exist + * @throws IllegalArgumentException if intent is null + * @throws SecurityException if no suitable permission is present + */ + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + public void requestSingleUpdate(@NonNull Criteria criteria, @NonNull PendingIntent intent) { checkCriteria(criteria); - try { - return mService.getBestProvider(criteria, enabledOnly); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + checkPendingIntent(intent); + + LocationRequest request = LocationRequest.createFromDeprecatedCriteria( + criteria, 0, 0, true); + requestLocationUpdates(request, intent); } /** @@ -524,7 +688,7 @@ public class LocationManager { LocationRequest request = LocationRequest.createFromDeprecatedProvider( provider, minTime, minDistance, false); - requestLocationUpdates(request, listener, null, null); + requestLocationUpdates(request, listener, null); } /** @@ -556,7 +720,36 @@ public class LocationManager { LocationRequest request = LocationRequest.createFromDeprecatedProvider( provider, minTime, minDistance, false); - requestLocationUpdates(request, listener, looper, null); + requestLocationUpdates(request, listener, looper); + } + + /** + * Register for location updates from the given provider with the given arguments. {@link + * LocationListener} callbacks will take place on the given {@link Executor}. Only one request + * can be registered for each unique listener, so any subsequent requests with the same listener + * will overwrite all associated arguments. + * + * <p>See {@link #requestLocationUpdates(String, long, float, LocationListener, Looper)} for + * more information. + * + * @param provider the name of the provider used for location updates + * @param minTimeMs minimum time interval between location updates, in milliseconds + * @param minDistanceM minimum distance between location updates, in meters + * @param executor all listener updates will take place on this {@link Executor} + * @param listener a {@link LocationListener} that will be called when updates are available + * @throws IllegalArgumentException if provider, listener, or looper is null or nonexistant + * @throws SecurityException if no suitable permission is present + */ + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + public void requestLocationUpdates( + @NonNull String provider, + long minTimeMs, + float minDistanceM, + @NonNull @CallbackExecutor Executor executor, + @NonNull LocationListener listener) { + LocationRequest request = LocationRequest.createFromDeprecatedProvider( + provider, minTimeMs, minDistanceM, false); + requestLocationUpdates(request, executor, listener); } /** @@ -589,7 +782,33 @@ public class LocationManager { LocationRequest request = LocationRequest.createFromDeprecatedCriteria( criteria, minTime, minDistance, false); - requestLocationUpdates(request, listener, looper, null); + requestLocationUpdates(request, listener, looper); + } + + /** + * Uses the given {@link Criteria} to select a single provider to use for location updates. + * + * <p>See {@link #requestLocationUpdates(String, long, float, Executor, LocationListener)} for + * more information. + * + * @param minTimeMs minimum time interval between location updates, in milliseconds + * @param minDistanceM minimum distance between location updates, in meters + * @param criteria the {@link Criteria} used to select a provider for location updates + * @param executor all listener updates will take place on this {@link Executor} + * @param listener a {@link LocationListener} that will be called when updates are available + * @throws IllegalArgumentException if criteria, listener, or looper is null + * @throws SecurityException if no suitable permission is present + */ + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + public void requestLocationUpdates( + long minTimeMs, + float minDistanceM, + @NonNull Criteria criteria, + @NonNull @CallbackExecutor Executor executor, + @NonNull LocationListener listener) { + LocationRequest request = LocationRequest.createFromDeprecatedCriteria( + criteria, minTimeMs, minDistanceM, false); + requestLocationUpdates(request, executor, listener); } /** @@ -617,7 +836,7 @@ public class LocationManager { LocationRequest request = LocationRequest.createFromDeprecatedProvider( provider, minTime, minDistance, false); - requestLocationUpdates(request, null, null, intent); + requestLocationUpdates(request, intent); } /** @@ -724,117 +943,7 @@ public class LocationManager { LocationRequest request = LocationRequest.createFromDeprecatedCriteria( criteria, minTime, minDistance, false); - requestLocationUpdates(request, null, null, intent); - } - - /** - * Register for a single location update using the named provider and - * a callback. - * - * <p>See {@link #requestLocationUpdates(long, float, Criteria, PendingIntent)} - * for more detail on how to use this method. - * - * @param provider the name of the provider with which to register - * @param listener a {@link LocationListener} whose - * {@link LocationListener#onLocationChanged} method will be called when - * the location update is available - * @param looper a Looper object whose message queue will be used to - * implement the callback mechanism, or null to make callbacks on the calling - * thread - * - * @throws IllegalArgumentException if provider is null or doesn't exist - * @throws IllegalArgumentException if listener is null - * @throws SecurityException if no suitable permission is present - */ - @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) - public void requestSingleUpdate( - @NonNull String provider, @NonNull LocationListener listener, @Nullable Looper looper) { - checkProvider(provider); - checkListener(listener); - - LocationRequest request = LocationRequest.createFromDeprecatedProvider( - provider, 0, 0, true); - requestLocationUpdates(request, listener, looper, null); - } - - /** - * Register for a single location update using a Criteria and - * a callback. - * - * <p>See {@link #requestLocationUpdates(long, float, Criteria, PendingIntent)} - * for more detail on how to use this method. - * - * @param criteria contains parameters for the location manager to choose the - * appropriate provider and parameters to compute the location - * @param listener a {@link LocationListener} whose - * {@link LocationListener#onLocationChanged} method will be called when - * the location update is available - * @param looper a Looper object whose message queue will be used to - * implement the callback mechanism, or null to make callbacks on the calling - * thread - * - * @throws IllegalArgumentException if criteria is null - * @throws IllegalArgumentException if listener is null - * @throws SecurityException if no suitable permission is present - */ - @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) - public void requestSingleUpdate( - @NonNull Criteria criteria, - @NonNull LocationListener listener, - @Nullable Looper looper) { - checkCriteria(criteria); - checkListener(listener); - - LocationRequest request = LocationRequest.createFromDeprecatedCriteria( - criteria, 0, 0, true); - requestLocationUpdates(request, listener, looper, null); - } - - /** - * Register for a single location update using a named provider and pending intent. - * - * <p>See {@link #requestLocationUpdates(long, float, Criteria, PendingIntent)} - * for more detail on how to use this method. - * - * @param provider the name of the provider with which to register - * @param intent a {@link PendingIntent} to be sent for the location update - * - * @throws IllegalArgumentException if provider is null or doesn't exist - * @throws IllegalArgumentException if intent is null - * @throws SecurityException if no suitable permission is present - */ - @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) - public void requestSingleUpdate(@NonNull String provider, @NonNull PendingIntent intent) { - checkProvider(provider); - checkPendingIntent(intent); - - LocationRequest request = LocationRequest.createFromDeprecatedProvider( - provider, 0, 0, true); - requestLocationUpdates(request, null, null, intent); - } - - /** - * Register for a single location update using a Criteria and pending intent. - * - * <p>See {@link #requestLocationUpdates(long, float, Criteria, PendingIntent)} - * for more detail on how to use this method. - * - * @param criteria contains parameters for the location manager to choose the - * appropriate provider and parameters to compute the location - * @param intent a {@link PendingIntent} to be sent for the location update - * - * @throws IllegalArgumentException if provider is null or doesn't exist - * @throws IllegalArgumentException if intent is null - * @throws SecurityException if no suitable permission is present - */ - @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) - public void requestSingleUpdate(@NonNull Criteria criteria, @NonNull PendingIntent intent) { - checkCriteria(criteria); - checkPendingIntent(intent); - - LocationRequest request = LocationRequest.createFromDeprecatedCriteria( - criteria, 0, 0, true); - requestLocationUpdates(request, null, null, intent); + requestLocationUpdates(request, intent); } /** @@ -881,7 +990,7 @@ public class LocationManager { * * <p> To unregister for Location updates, use: {@link #removeUpdates(LocationListener)}. * - * @param request quality of service required, null for default low power + * @param locationRequest quality of service required, null for default low power * @param listener a {@link LocationListener} whose * {@link LocationListener#onLocationChanged} method will be called when * the location update is available @@ -898,13 +1007,37 @@ public class LocationManager { @TestApi @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) public void requestLocationUpdates( - @NonNull LocationRequest request, + @NonNull LocationRequest locationRequest, @NonNull LocationListener listener, @Nullable Looper looper) { - checkListener(listener); - requestLocationUpdates(request, listener, looper, null); + requestLocationUpdates(locationRequest, + new LocationListenerTransport(looper == null ? new Handler() : new Handler(looper), + listener)); } + /** + * Register for location updates with the given {@link LocationRequest}. + * + * <p>See {@link #requestLocationUpdates(String, long, float, Executor, LocationListener)} for + * more information. + * + * @param locationRequest the {@link LocationRequest} being made + * @param executor all listener updates will take place on this {@link Executor} + * @param listener a {@link LocationListener} that will be called when updates are + * available + * @throws IllegalArgumentException if locationRequest, listener, or executor is null + * @throws SecurityException if no suitable permission is present + * @hide + */ + @SystemApi + @TestApi + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + public void requestLocationUpdates( + @NonNull LocationRequest locationRequest, + @NonNull @CallbackExecutor Executor executor, + @NonNull LocationListener listener) { + requestLocationUpdates(locationRequest, new LocationListenerTransport(executor, listener)); + } /** * Register for fused location updates using a LocationRequest and a pending intent. @@ -918,8 +1051,8 @@ public class LocationManager { * <p> See {@link #requestLocationUpdates(LocationRequest, LocationListener, Looper)} * for more detail. * - * @param request quality of service required, null for default low power - * @param intent a {@link PendingIntent} to be sent for the location update + * @param locationRequest quality of service required, null for default low power + * @param pendingIntent a {@link PendingIntent} to be sent for the location update * * @throws IllegalArgumentException if intent is null * @throws SecurityException if no suitable permission is present @@ -930,9 +1063,38 @@ public class LocationManager { @TestApi @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) public void requestLocationUpdates( - @NonNull LocationRequest request, @NonNull PendingIntent intent) { - checkPendingIntent(intent); - requestLocationUpdates(request, null, null, intent); + @NonNull LocationRequest locationRequest, + @NonNull PendingIntent pendingIntent) { + Preconditions.checkArgument(locationRequest != null, "invalid null location request"); + Preconditions.checkArgument(pendingIntent != null, "invalid null pending intent"); + if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.JELLY_BEAN) { + Preconditions.checkArgument(pendingIntent.isTargetedToPackage(), + "pending intent must be targeted to package"); + } + + try { + mService.requestLocationUpdates(locationRequest, null, pendingIntent, + mContext.getPackageName()); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + private void requestLocationUpdates(LocationRequest request, + LocationListenerTransport transport) { + synchronized (mListeners) { + LocationListenerTransport oldTransport = mListeners.put(transport.getKey(), transport); + if (oldTransport != null) { + oldTransport.unregisterListener(); + } + + try { + mService.requestLocationUpdates(request, transport, null, + mContext.getPackageName()); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } } /** @@ -963,443 +1125,185 @@ public class LocationManager { } } - private ListenerTransport wrapListener(LocationListener listener, Looper looper) { - if (listener == null) return null; - synchronized (mListeners) { - ListenerTransport transport = mListeners.get(listener); - if (transport == null) { - transport = new ListenerTransport(listener, looper); - } - mListeners.put(listener, transport); - return transport; - } - } - - @UnsupportedAppUsage - private void requestLocationUpdates(LocationRequest request, LocationListener listener, - Looper looper, PendingIntent intent) { - - String packageName = mContext.getPackageName(); - - // wrap the listener class - ListenerTransport transport = wrapListener(listener, looper); - - try { - mService.requestLocationUpdates(request, transport, intent, packageName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - } - /** - * Removes all location updates for the specified LocationListener. + * Removes location updates for the specified LocationListener. Following this call, updates + * will no longer occur for this listener. * - * <p>Following this call, updates will no longer - * occur for this listener. - * - * @param listener listener object that no longer needs location updates + * @param listener listener that no longer needs location updates * @throws IllegalArgumentException if listener is null */ public void removeUpdates(@NonNull LocationListener listener) { - checkListener(listener); - String packageName = mContext.getPackageName(); + Preconditions.checkArgument(listener != null, "invalid null listener"); - ListenerTransport transport; synchronized (mListeners) { - transport = mListeners.remove(listener); - } - if (transport == null) return; - - try { - mService.removeUpdates(transport, null, packageName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - } - - /** - * Removes all location updates for the specified pending intent. - * - * <p>Following this call, updates will no longer for this pending intent. - * - * @param intent pending intent object that no longer needs location updates - * @throws IllegalArgumentException if intent is null - */ - public void removeUpdates(@NonNull PendingIntent intent) { - checkPendingIntent(intent); - String packageName = mContext.getPackageName(); - - try { - mService.removeUpdates(null, intent, packageName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - } - - /** - * Set a proximity alert for the location given by the position - * (latitude, longitude) and the given radius. - * - * <p> When the device - * detects that it has entered or exited the area surrounding the - * location, the given PendingIntent will be used to create an Intent - * to be fired. - * - * <p> The fired Intent will have a boolean extra added with key - * {@link #KEY_PROXIMITY_ENTERING}. If the value is true, the device is - * entering the proximity region; if false, it is exiting. - * - * <p> Due to the approximate nature of position estimation, if the - * device passes through the given area briefly, it is possible - * that no Intent will be fired. Similarly, an Intent could be - * fired if the device passes very close to the given area but - * does not actually enter it. - * - * <p> After the number of milliseconds given by the expiration - * parameter, the location manager will delete this proximity - * alert and no longer monitor it. A value of -1 indicates that - * there should be no expiration time. - * - * <p> Internally, this method uses both {@link #NETWORK_PROVIDER} - * and {@link #GPS_PROVIDER}. - * - * <p>Before API version 17, this method could be used with - * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} or - * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}. - * From API version 17 and onwards, this method requires - * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission. - * - * @param latitude the latitude of the central point of the - * alert region - * @param longitude the longitude of the central point of the - * alert region - * @param radius the radius of the central point of the - * alert region, in meters - * @param expiration time for this proximity alert, in milliseconds, - * or -1 to indicate no expiration - * @param intent a PendingIntent that will be used to generate an Intent to - * fire when entry to or exit from the alert region is detected - * - * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} - * permission is not present - */ - @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) - public void addProximityAlert(double latitude, double longitude, float radius, long expiration, - @NonNull PendingIntent intent) { - checkPendingIntent(intent); - if (expiration < 0) expiration = Long.MAX_VALUE; + LocationListenerTransport transport = mListeners.remove(listener); + if (transport == null) { + return; + } + transport.unregisterListener(); - Geofence fence = Geofence.createCircle(latitude, longitude, radius); - LocationRequest request = new LocationRequest().setExpireIn(expiration); - try { - mService.requestGeofence(request, fence, intent, mContext.getPackageName()); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); + try { + mService.removeUpdates(transport, null, mContext.getPackageName()); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } } } /** - * Add a geofence with the specified LocationRequest quality of service. - * - * <p> When the device - * detects that it has entered or exited the area surrounding the - * location, the given PendingIntent will be used to create an Intent - * to be fired. - * - * <p> The fired Intent will have a boolean extra added with key - * {@link #KEY_PROXIMITY_ENTERING}. If the value is true, the device is - * entering the proximity region; if false, it is exiting. + * Removes all location updates for the specified pending intent. Following this call, updates + * will no longer occur for this pending intent. * - * <p> The geofence engine fuses results from all location providers to - * provide the best balance between accuracy and power. Applications - * can choose the quality of service required using the - * {@link LocationRequest} object. If it is null then a default, - * low power geo-fencing implementation is used. It is possible to cross - * a geo-fence without notification, but the system will do its best - * to detect, using {@link LocationRequest} as a hint to trade-off - * accuracy and power. - * - * <p> The power required by the geofence engine can depend on many factors, - * such as quality and interval requested in {@link LocationRequest}, - * distance to nearest geofence and current device velocity. - * - * @param request quality of service required, null for default low power - * @param fence a geographical description of the geofence area - * @param intent pending intent to receive geofence updates - * - * @throws IllegalArgumentException if fence is null - * @throws IllegalArgumentException if intent is null - * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} - * permission is not present - * - * @hide + * @param pendingIntent pending intent that no longer needs location updates + * @throws IllegalArgumentException if pendingIntent is null */ - @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) - public void addGeofence( - @NonNull LocationRequest request, - @NonNull Geofence fence, - @NonNull PendingIntent intent) { - checkPendingIntent(intent); - checkGeofence(fence); + public void removeUpdates(@NonNull PendingIntent pendingIntent) { + Preconditions.checkArgument(pendingIntent != null, "invalid null pending intent"); try { - mService.requestGeofence(request, fence, intent, mContext.getPackageName()); + mService.removeUpdates(null, pendingIntent, mContext.getPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Removes the proximity alert with the given PendingIntent. - * - * <p>Before API version 17, this method could be used with - * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} or - * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}. - * From API version 17 and onwards, this method requires - * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission. - * - * @param intent the PendingIntent that no longer needs to be notified of - * proximity alerts + * Returns a list of the names of all known location providers. + * <p>All providers are returned, including ones that are not permitted to + * be accessed by the calling activity or are currently disabled. * - * @throws IllegalArgumentException if intent is null - * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} - * permission is not present + * @return list of Strings containing names of the provider */ - public void removeProximityAlert(@NonNull PendingIntent intent) { - checkPendingIntent(intent); - String packageName = mContext.getPackageName(); - + public @NonNull List<String> getAllProviders() { try { - mService.removeGeofence(null, intent, packageName); + return mService.getAllProviders(); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Remove a single geofence. - * - * <p>This removes only the specified geofence associated with the - * specified pending intent. All other geofences remain unchanged. - * - * @param fence a geofence previously passed to {@link #addGeofence} - * @param intent a pending intent previously passed to {@link #addGeofence} - * - * @throws IllegalArgumentException if fence is null - * @throws IllegalArgumentException if intent is null - * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} - * permission is not present + * Returns a list of the names of location providers. * - * @hide + * @param enabledOnly if true then only the providers which are currently + * enabled are returned. + * @return list of Strings containing names of the providers */ - public void removeGeofence(@NonNull Geofence fence, @NonNull PendingIntent intent) { - checkPendingIntent(intent); - checkGeofence(fence); - String packageName = mContext.getPackageName(); - + public @NonNull List<String> getProviders(boolean enabledOnly) { try { - mService.removeGeofence(fence, intent, packageName); + return mService.getProviders(null, enabledOnly); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Remove all geofences registered to the specified pending intent. - * - * @param intent a pending intent previously passed to {@link #addGeofence} - * - * @throws IllegalArgumentException if intent is null - * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} - * permission is not present + * Returns a list of the names of LocationProviders that satisfy the given + * criteria, or null if none do. Only providers that are permitted to be + * accessed by the calling activity will be returned. * - * @hide + * @param criteria the criteria that the returned providers must match + * @param enabledOnly if true then only the providers which are currently + * enabled are returned. + * @return list of Strings containing names of the providers */ - public void removeAllGeofences(@NonNull PendingIntent intent) { - checkPendingIntent(intent); - String packageName = mContext.getPackageName(); - + public @NonNull List<String> getProviders(@NonNull Criteria criteria, boolean enabledOnly) { + checkCriteria(criteria); try { - mService.removeGeofence(null, intent, packageName); + return mService.getProviders(criteria, enabledOnly); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Returns the current enabled/disabled state of location. To listen for changes, see - * {@link #MODE_CHANGED_ACTION}. + * Returns the name of the provider that best meets the given criteria. Only providers + * that are permitted to be accessed by the calling activity will be + * returned. If several providers meet the criteria, the one with the best + * accuracy is returned. If no provider meets the criteria, + * the criteria are loosened in the following sequence: * - * @return true if location is enabled and false if location is disabled. - */ - public boolean isLocationEnabled() { - return isLocationEnabledForUser(Process.myUserHandle()); - } - - /** - * Returns the current enabled/disabled state of location. + * <ul> + * <li> power requirement + * <li> accuracy + * <li> bearing + * <li> speed + * <li> altitude + * </ul> * - * @param userHandle the user to query - * @return true if location is enabled and false if location is disabled. + * <p> Note that the requirement on monetary cost is not removed + * in this process. * - * @hide + * @param criteria the criteria that need to be matched + * @param enabledOnly if true then only a provider that is currently enabled is returned + * @return name of the provider that best matches the requirements */ - @SystemApi - public boolean isLocationEnabledForUser(@NonNull UserHandle userHandle) { + public @Nullable String getBestProvider(@NonNull Criteria criteria, boolean enabledOnly) { + checkCriteria(criteria); try { - return mService.isLocationEnabledForUser(userHandle.getIdentifier()); + return mService.getBestProvider(criteria, enabledOnly); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Enables or disables the location setting. - * - * @param enabled true to enable location and false to disable location. - * @param userHandle the user to set - * - * @hide - */ - @SystemApi - @TestApi - @RequiresPermission(WRITE_SECURE_SETTINGS) - public void setLocationEnabledForUser(boolean enabled, @NonNull UserHandle userHandle) { - Settings.Secure.putIntForUser( - mContext.getContentResolver(), - Settings.Secure.LOCATION_MODE, - enabled - ? Settings.Secure.LOCATION_MODE_ON - : Settings.Secure.LOCATION_MODE_OFF, - userHandle.getIdentifier()); - } - - /** - * Returns the current enabled/disabled status of the given provider. To listen for changes, see - * {@link #PROVIDERS_CHANGED_ACTION}. - * - * Before API version {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method would throw - * {@link SecurityException} if the location permissions were not sufficient to use the - * specified provider. - * - * @param provider the name of the provider - * @return true if the provider exists and is enabled - * - * @throws IllegalArgumentException if provider is null - */ - public boolean isProviderEnabled(@NonNull String provider) { - return isProviderEnabledForUser(provider, Process.myUserHandle()); - } - - /** - * Returns the current enabled/disabled status of the given provider and user. Callers should - * prefer {@link #isLocationEnabledForUser(UserHandle)} unless they depend on provider-specific - * APIs. - * - * Before API version {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method would throw - * {@link SecurityException} if the location permissions were not sufficient to use the - * specified provider. + * Returns the information associated with the location provider of the + * given name, or null if no provider exists by that name. * - * @param provider the name of the provider - * @param userHandle the user to query - * @return true if the provider exists and is enabled + * @param name the provider name + * @return a LocationProvider, or null * - * @throws IllegalArgumentException if provider is null - * @hide + * @throws IllegalArgumentException if name is null or does not exist + * @throws SecurityException if the caller is not permitted to access the + * given provider. */ - @SystemApi - public boolean isProviderEnabledForUser( - @NonNull String provider, @NonNull UserHandle userHandle) { - checkProvider(provider); - + public @Nullable LocationProvider getProvider(@NonNull String name) { + checkProvider(name); try { - return mService.isProviderEnabledForUser(provider, userHandle.getIdentifier()); + ProviderProperties properties = mService.getProviderProperties(name); + if (properties == null) { + return null; + } + return new LocationProvider(name, properties); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Method for enabling or disabling a single location provider. This method is deprecated and - * functions as a best effort. It should not be relied on in any meaningful sense as providers - * may no longer be enabled or disabled by clients. - * - * @param provider the name of the provider - * @param enabled true to enable the provider. false to disable the provider - * @param userHandle the user to set - * @return true if the value was set, false otherwise + * Returns true if the given package name matches a location provider package, and false + * otherwise. * - * @throws IllegalArgumentException if provider is null - * @deprecated Do not manipulate providers individually, use - * {@link #setLocationEnabledForUser(boolean, UserHandle)} instead. * @hide */ - @Deprecated @SystemApi - @RequiresPermission(WRITE_SECURE_SETTINGS) - public boolean setProviderEnabledForUser( - @NonNull String provider, boolean enabled, @NonNull UserHandle userHandle) { - checkProvider(provider); - - return Settings.Secure.putStringForUser( - mContext.getContentResolver(), - Settings.Secure.LOCATION_PROVIDERS_ALLOWED, - (enabled ? "+" : "-") + provider, - userHandle.getIdentifier()); - } - - /** - * Get the last known location. - * - * <p>This location could be very old so use - * {@link Location#getElapsedRealtimeNanos} to calculate its age. It can - * also return null if no previous location is available. - * - * <p>Always returns immediately. - * - * @return The last known location, or null if not available - * @throws SecurityException if no suitable permission is present - * - * @hide - */ - @Nullable - public Location getLastLocation() { - String packageName = mContext.getPackageName(); - + @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG) + public boolean isProviderPackage(@NonNull String packageName) { try { - return mService.getLastLocation(null, packageName); + return mService.isProviderPackage(packageName); } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); + e.rethrowFromSystemServer(); + return false; } } /** - * Returns a Location indicating the data from the last known - * location fix obtained from the given provider. - * - * <p> This can be done - * without starting the provider. Note that this location could - * be out-of-date, for example if the device was turned off and - * moved to another location. - * - * <p> If the provider is currently disabled, null is returned. - * - * @param provider the name of the provider - * @return the last known location for the provider, or null + * Sends additional commands to a location provider. Can be used to support provider specific + * extensions to the Location Manager API. * - * @throws SecurityException if no suitable permission is present - * @throws IllegalArgumentException if provider is null or doesn't exist + * @param provider name of the location provider. + * @param command name of the command to send to the provider. + * @param extras optional arguments for the command (or null). + * @return true always */ - @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) - @Nullable - public Location getLastKnownLocation(@NonNull String provider) { - checkProvider(provider); - String packageName = mContext.getPackageName(); - LocationRequest request = LocationRequest.createFromDeprecatedProvider( - provider, 0, 0, true); + public boolean sendExtraCommand( + @NonNull String provider, @NonNull String command, @Nullable Bundle extras) { + Preconditions.checkArgument(provider != null, "invalid null provider"); + Preconditions.checkArgument(command != null, "invalid null command"); try { - return mService.getLastLocation(request, packageName); + return mService.sendExtraCommand(provider, command, extras); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -1574,199 +1478,308 @@ public class LocationManager { } } - // --- GPS-specific support --- - - // This class is used to send Gnss status events to the client's specific thread. - private class GnssStatusListenerTransport extends IGnssStatusListener.Stub { - - private final GnssStatus.Callback mGnssCallback; - private final OnNmeaMessageListener mGnssNmeaListener; - - private class GnssHandler extends Handler { - GnssHandler(Handler handler) { - super(handler != null ? handler.getLooper() : Looper.myLooper()); - } + /** + * Set a proximity alert for the location given by the position + * (latitude, longitude) and the given radius. + * + * <p> When the device + * detects that it has entered or exited the area surrounding the + * location, the given PendingIntent will be used to create an Intent + * to be fired. + * + * <p> The fired Intent will have a boolean extra added with key + * {@link #KEY_PROXIMITY_ENTERING}. If the value is true, the device is + * entering the proximity region; if false, it is exiting. + * + * <p> Due to the approximate nature of position estimation, if the + * device passes through the given area briefly, it is possible + * that no Intent will be fired. Similarly, an Intent could be + * fired if the device passes very close to the given area but + * does not actually enter it. + * + * <p> After the number of milliseconds given by the expiration + * parameter, the location manager will delete this proximity + * alert and no longer monitor it. A value of -1 indicates that + * there should be no expiration time. + * + * <p> Internally, this method uses both {@link #NETWORK_PROVIDER} + * and {@link #GPS_PROVIDER}. + * + * <p>Before API version 17, this method could be used with + * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} or + * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}. + * From API version 17 and onwards, this method requires + * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission. + * + * @param latitude the latitude of the central point of the + * alert region + * @param longitude the longitude of the central point of the + * alert region + * @param radius the radius of the central point of the + * alert region, in meters + * @param expiration time for this proximity alert, in milliseconds, + * or -1 to indicate no expiration + * @param intent a PendingIntent that will be used to generate an Intent to + * fire when entry to or exit from the alert region is detected + * + * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} + * permission is not present + */ + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + public void addProximityAlert(double latitude, double longitude, float radius, long expiration, + @NonNull PendingIntent intent) { + checkPendingIntent(intent); + if (expiration < 0) expiration = Long.MAX_VALUE; - @Override - public void handleMessage(Message msg) { - switch (msg.what) { - case NMEA_RECEIVED: - synchronized (mNmeaBuffer) { - for (Nmea nmea : mNmeaBuffer) { - mGnssNmeaListener.onNmeaMessage(nmea.mNmea, nmea.mTimestamp); - } - mNmeaBuffer.clear(); - } - break; - case GNSS_EVENT_STARTED: - mGnssCallback.onStarted(); - break; - case GNSS_EVENT_STOPPED: - mGnssCallback.onStopped(); - break; - case GNSS_EVENT_FIRST_FIX: - mGnssCallback.onFirstFix(mTimeToFirstFix); - break; - case GNSS_EVENT_SATELLITE_STATUS: - mGnssCallback.onSatelliteStatusChanged(mGnssStatus); - break; - default: - break; - } - } + Geofence fence = Geofence.createCircle(latitude, longitude, radius); + LocationRequest request = new LocationRequest().setExpireIn(expiration); + try { + mService.requestGeofence(request, fence, intent, mContext.getPackageName()); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } + } - private final Handler mGnssHandler; - - private static final int NMEA_RECEIVED = 1; - private static final int GNSS_EVENT_STARTED = 2; - private static final int GNSS_EVENT_STOPPED = 3; - private static final int GNSS_EVENT_FIRST_FIX = 4; - private static final int GNSS_EVENT_SATELLITE_STATUS = 5; - - private class Nmea { - long mTimestamp; - String mNmea; + /** + * Removes the proximity alert with the given PendingIntent. + * + * <p>Before API version 17, this method could be used with + * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} or + * {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}. + * From API version 17 and onwards, this method requires + * {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission. + * + * @param intent the PendingIntent that no longer needs to be notified of + * proximity alerts + * + * @throws IllegalArgumentException if intent is null + * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} + * permission is not present + */ + public void removeProximityAlert(@NonNull PendingIntent intent) { + checkPendingIntent(intent); + String packageName = mContext.getPackageName(); - Nmea(long timestamp, String nmea) { - mTimestamp = timestamp; - mNmea = nmea; - } + try { + mService.removeGeofence(null, intent, packageName); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } - private final ArrayList<Nmea> mNmeaBuffer; + } - GnssStatusListenerTransport(GnssStatus.Callback callback, Handler handler) { - mGnssCallback = callback; - mGnssHandler = new GnssHandler(handler); - mGnssNmeaListener = null; - mNmeaBuffer = null; - } + /** + * Add a geofence with the specified LocationRequest quality of service. + * + * <p> When the device + * detects that it has entered or exited the area surrounding the + * location, the given PendingIntent will be used to create an Intent + * to be fired. + * + * <p> The fired Intent will have a boolean extra added with key + * {@link #KEY_PROXIMITY_ENTERING}. If the value is true, the device is + * entering the proximity region; if false, it is exiting. + * + * <p> The geofence engine fuses results from all location providers to + * provide the best balance between accuracy and power. Applications + * can choose the quality of service required using the + * {@link LocationRequest} object. If it is null then a default, + * low power geo-fencing implementation is used. It is possible to cross + * a geo-fence without notification, but the system will do its best + * to detect, using {@link LocationRequest} as a hint to trade-off + * accuracy and power. + * + * <p> The power required by the geofence engine can depend on many factors, + * such as quality and interval requested in {@link LocationRequest}, + * distance to nearest geofence and current device velocity. + * + * @param request quality of service required, null for default low power + * @param fence a geographical description of the geofence area + * @param intent pending intent to receive geofence updates + * + * @throws IllegalArgumentException if fence is null + * @throws IllegalArgumentException if intent is null + * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} + * permission is not present + * + * @hide + */ + @RequiresPermission(anyOf = {ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION}) + public void addGeofence( + @NonNull LocationRequest request, + @NonNull Geofence fence, + @NonNull PendingIntent intent) { + checkPendingIntent(intent); + checkGeofence(fence); - GnssStatusListenerTransport(OnNmeaMessageListener listener, Handler handler) { - mGnssCallback = null; - mGnssHandler = new GnssHandler(handler); - mGnssNmeaListener = listener; - mNmeaBuffer = new ArrayList<>(); + try { + mService.requestGeofence(request, fence, intent, mContext.getPackageName()); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } + } - GnssStatusListenerTransport(GpsStatus.Listener listener, Handler handler) { - mGnssHandler = new GnssHandler(handler); - mNmeaBuffer = null; - mGnssCallback = listener != null ? new GnssStatus.Callback() { - @Override - public void onStarted() { - listener.onGpsStatusChanged(GpsStatus.GPS_EVENT_STARTED); - } + /** + * Remove a single geofence. + * + * <p>This removes only the specified geofence associated with the + * specified pending intent. All other geofences remain unchanged. + * + * @param fence a geofence previously passed to {@link #addGeofence} + * @param intent a pending intent previously passed to {@link #addGeofence} + * + * @throws IllegalArgumentException if fence is null + * @throws IllegalArgumentException if intent is null + * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} + * permission is not present + * + * @hide + */ + public void removeGeofence(@NonNull Geofence fence, @NonNull PendingIntent intent) { + checkPendingIntent(intent); + checkGeofence(fence); + String packageName = mContext.getPackageName(); - @Override - public void onStopped() { - listener.onGpsStatusChanged(GpsStatus.GPS_EVENT_STOPPED); - } + try { + mService.removeGeofence(fence, intent, packageName); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } - @Override - public void onFirstFix(int ttff) { - listener.onGpsStatusChanged(GpsStatus.GPS_EVENT_FIRST_FIX); - } + /** + * Remove all geofences registered to the specified pending intent. + * + * @param intent a pending intent previously passed to {@link #addGeofence} + * + * @throws IllegalArgumentException if intent is null + * @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION} + * permission is not present + * + * @hide + */ + public void removeAllGeofences(@NonNull PendingIntent intent) { + checkPendingIntent(intent); + String packageName = mContext.getPackageName(); - @Override - public void onSatelliteStatusChanged(GnssStatus status) { - listener.onGpsStatusChanged(GpsStatus.GPS_EVENT_SATELLITE_STATUS); - } - } : null; - mGnssNmeaListener = null; + try { + mService.removeGeofence(null, intent, packageName); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } + } - @Override - public void onGnssStarted() { - if (mGnssCallback != null) { - mGnssHandler.obtainMessage(GNSS_EVENT_STARTED).sendToTarget(); - } - } + // ================= GNSS APIs ================= - @Override - public void onGnssStopped() { - if (mGnssCallback != null) { - mGnssHandler.obtainMessage(GNSS_EVENT_STOPPED).sendToTarget(); + /** + * Returns the supported capabilities of the GNSS chipset. + * + * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present. + * + * @hide + */ + @SystemApi + @RequiresPermission(ACCESS_FINE_LOCATION) + public @NonNull GnssCapabilities getGnssCapabilities() { + try { + long gnssCapabilities = mService.getGnssCapabilities(mContext.getPackageName()); + if (gnssCapabilities == GnssCapabilities.INVALID_CAPABILITIES) { + gnssCapabilities = 0L; } + return GnssCapabilities.of(gnssCapabilities); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } + } - @Override - public void onFirstFix(int ttff) { - if (mGnssCallback != null) { - mTimeToFirstFix = ttff; - mGnssHandler.obtainMessage(GNSS_EVENT_FIRST_FIX).sendToTarget(); - } + /** + * Returns the model year of the GNSS hardware and software build. More details, such as build + * date, may be available in {@link #getGnssHardwareModelName()}. May return 0 if the model year + * is less than 2016. + */ + public int getGnssYearOfHardware() { + try { + return mService.getGnssYearOfHardware(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } + } - @Override - public void onSvStatusChanged(int svCount, int[] prnWithFlags, - float[] cn0s, float[] elevations, float[] azimuths, float[] carrierFreqs) { - if (mGnssCallback != null) { - mGnssStatus = new GnssStatus(svCount, prnWithFlags, cn0s, elevations, azimuths, - carrierFreqs); - - mGnssHandler.removeMessages(GNSS_EVENT_SATELLITE_STATUS); - mGnssHandler.obtainMessage(GNSS_EVENT_SATELLITE_STATUS).sendToTarget(); - } + /** + * Returns the Model Name (including Vendor and Hardware/Software Version) of the GNSS hardware + * driver. + * + * <p> No device-specific serial number or ID is returned from this API. + * + * <p> Will return null when the GNSS hardware abstraction layer does not support providing + * this value. + */ + @Nullable + public String getGnssHardwareModelName() { + try { + return mService.getGnssHardwareModelName(); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } + } - @Override - public void onNmeaReceived(long timestamp, String nmea) { - if (mGnssNmeaListener != null) { - synchronized (mNmeaBuffer) { - mNmeaBuffer.add(new Nmea(timestamp, nmea)); - } - - mGnssHandler.removeMessages(NMEA_RECEIVED); - mGnssHandler.obtainMessage(NMEA_RECEIVED).sendToTarget(); - } + /** + * Retrieves information about the current status of the GPS engine. + * This should only be called from the {@link GpsStatus.Listener#onGpsStatusChanged} + * callback to ensure that the data is copied atomically. + * + * The caller may either pass in a {@link GpsStatus} object to set with the latest + * status information, or pass null to create a new {@link GpsStatus} object. + * + * @param status object containing GPS status details, or null. + * @return status object containing updated GPS status. + * @deprecated GpsStatus APIs are deprecated, use {@link GnssStatus} APIs instead. + */ + @Deprecated + @RequiresPermission(ACCESS_FINE_LOCATION) + public @Nullable GpsStatus getGpsStatus(@Nullable GpsStatus status) { + if (status == null) { + status = new GpsStatus(); } + // When mGnssStatus is null, that means that this method is called outside + // onGpsStatusChanged(). Return an empty status to maintain backwards compatibility. + GnssStatus gnssStatus = mGnssStatusListenerManager.getGnssStatus(); + int ttff = mGnssStatusListenerManager.getTtff(); + if (gnssStatus != null) { + status.setStatus(gnssStatus, ttff); + } + return status; } /** * Adds a GPS status listener. * * @param listener GPS status listener object to register - * * @return true if the listener was successfully added - * * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present - * @deprecated use {@link #registerGnssStatusCallback(GnssStatus.Callback)} instead. + * @deprecated use {@link #registerGnssStatusCallback(GnssStatus.Callback)} instead. No longer + * supported in apps targeting R and above. */ @Deprecated @RequiresPermission(ACCESS_FINE_LOCATION) public boolean addGpsStatusListener(GpsStatus.Listener listener) { - boolean result; - - if (mGpsStatusListeners.get(listener) != null) { - return true; - } try { - GnssStatusListenerTransport transport = new GnssStatusListenerTransport(listener, null); - result = mService.registerGnssStatusCallback(transport, mContext.getPackageName()); - if (result) { - mGpsStatusListeners.put(listener, transport); - } + return mGnssStatusListenerManager.addListener(listener, new Handler()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } - - return result; } /** * Removes a GPS status listener. * * @param listener GPS status listener object to remove - * @deprecated use {@link #unregisterGnssStatusCallback(GnssStatus.Callback)} instead. + * @deprecated use {@link #unregisterGnssStatusCallback(GnssStatus.Callback)} instead. No longer + * supported in apps targeting R and above. */ @Deprecated public void removeGpsStatusListener(GpsStatus.Listener listener) { try { - GnssStatusListenerTransport transport = mGpsStatusListeners.remove(listener); - if (transport != null) { - mService.unregisterGnssStatusCallback(transport); - } + mGnssStatusListenerManager.removeListener(listener); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -1776,11 +1789,12 @@ public class LocationManager { * Registers a GNSS status callback. * * @param callback GNSS status callback object to register - * * @return true if the listener was successfully added - * * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present + * @deprecated Use {@link #registerGnssStatusCallback(GnssStatus.Callback, Handler)} or {@link + * #registerGnssStatusCallback(Executor, GnssStatus.Callback)} instead. */ + @Deprecated @RequiresPermission(ACCESS_FINE_LOCATION) public boolean registerGnssStatusCallback(@NonNull GnssStatus.Callback callback) { return registerGnssStatusCallback(callback, null); @@ -1790,33 +1804,41 @@ public class LocationManager { * Registers a GNSS status callback. * * @param callback GNSS status callback object to register - * @param handler the handler that the callback runs on. - * + * @param handler a handler with a looper that the callback runs on. * @return true if the listener was successfully added - * * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present */ @RequiresPermission(ACCESS_FINE_LOCATION) public boolean registerGnssStatusCallback( @NonNull GnssStatus.Callback callback, @Nullable Handler handler) { - boolean result; - synchronized (mGnssStatusListeners) { - if (mGnssStatusListeners.get(callback) != null) { - return true; - } - try { - GnssStatusListenerTransport transport = - new GnssStatusListenerTransport(callback, handler); - result = mService.registerGnssStatusCallback(transport, mContext.getPackageName()); - if (result) { - mGnssStatusListeners.put(callback, transport); - } - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + if (handler == null) { + handler = new Handler(); } - return result; + try { + return mGnssStatusListenerManager.addListener(callback, handler); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + + /** + * Registers a GNSS status callback. + * + * @param callback GNSS status callback object to register + * @param executor the executor that the callback runs on. + * @return true if the listener was successfully added + * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present + */ + @RequiresPermission(ACCESS_FINE_LOCATION) + public boolean registerGnssStatusCallback( + @NonNull @CallbackExecutor Executor executor, + @NonNull GnssStatus.Callback callback) { + try { + return mGnssStatusListenerManager.addListener(callback, executor); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } } /** @@ -1825,15 +1847,10 @@ public class LocationManager { * @param callback GNSS status callback object to remove */ public void unregisterGnssStatusCallback(@NonNull GnssStatus.Callback callback) { - synchronized (mGnssStatusListeners) { - try { - GnssStatusListenerTransport transport = mGnssStatusListeners.remove(callback); - if (transport != null) { - mService.unregisterGnssStatusCallback(transport); - } - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + try { + mGnssStatusListenerManager.removeListener(callback); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } } @@ -1868,11 +1885,12 @@ public class LocationManager { * Adds an NMEA listener. * * @param listener a {@link OnNmeaMessageListener} object to register - * * @return true if the listener was successfully added - * * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present + * @deprecated Use {@link #addNmeaListener(OnNmeaMessageListener, Handler)} or {@link + * #addNmeaListener(Executor, OnNmeaMessageListener)} instead. */ + @Deprecated @RequiresPermission(ACCESS_FINE_LOCATION) public boolean addNmeaListener(@NonNull OnNmeaMessageListener listener) { return addNmeaListener(listener, null); @@ -1882,33 +1900,40 @@ public class LocationManager { * Adds an NMEA listener. * * @param listener a {@link OnNmeaMessageListener} object to register - * @param handler the handler that the listener runs on. - * + * @param handler a handler with the looper that the listener runs on. * @return true if the listener was successfully added - * * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present */ @RequiresPermission(ACCESS_FINE_LOCATION) public boolean addNmeaListener( @NonNull OnNmeaMessageListener listener, @Nullable Handler handler) { - boolean result; - - if (mGnssNmeaListeners.get(listener) != null) { - // listener is already registered - return true; + if (handler == null) { + handler = new Handler(); } try { - GnssStatusListenerTransport transport = - new GnssStatusListenerTransport(listener, handler); - result = mService.registerGnssStatusCallback(transport, mContext.getPackageName()); - if (result) { - mGnssNmeaListeners.put(listener, transport); - } + return mGnssStatusListenerManager.addListener(listener, handler); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } + } - return result; + /** + * Adds an NMEA listener. + * + * @param listener a {@link OnNmeaMessageListener} object to register + * @param executor the {@link Executor} that the listener runs on. + * @return true if the listener was successfully added + * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present + */ + @RequiresPermission(ACCESS_FINE_LOCATION) + public boolean addNmeaListener( + @NonNull @CallbackExecutor Executor executor, + @NonNull OnNmeaMessageListener listener) { + try { + return mGnssStatusListenerManager.addListener(listener, executor); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } } /** @@ -1918,10 +1943,7 @@ public class LocationManager { */ public void removeNmeaListener(@NonNull OnNmeaMessageListener listener) { try { - GnssStatusListenerTransport transport = mGnssNmeaListeners.remove(listener); - if (transport != null) { - mService.unregisterGnssStatusCallback(transport); - } + mGnssStatusListenerManager.removeListener(listener); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -1942,11 +1964,29 @@ public class LocationManager { } /** + * No-op method to keep backward-compatibility. Don't use it. Use {@link + * #unregisterGnssMeasurementsCallback} instead. + * + * @hide + * @deprecated use {@link #unregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback)} + * instead. + * @removed + */ + @Deprecated + @SystemApi + @SuppressLint("Doclava125") + public void removeGpsMeasurementListener(GpsMeasurementsEvent.Listener listener) {} + + /** * Registers a GPS Measurement callback. * * @param callback a {@link GnssMeasurementsEvent.Callback} object to register. * @return {@code true} if the callback was added successfully, {@code false} otherwise. + * @deprecated Use {@link + * #registerGnssMeasurementsCallback(GnssMeasurementsEvent.Callback, Handler)} or {@link + * #registerGnssMeasurementsCallback(Executor, GnssMeasurementsEvent.Callback)} instead. */ + @Deprecated @RequiresPermission(ACCESS_FINE_LOCATION) public boolean registerGnssMeasurementsCallback( @NonNull GnssMeasurementsEvent.Callback callback) { @@ -1957,77 +1997,72 @@ public class LocationManager { * Registers a GPS Measurement callback. * * @param callback a {@link GnssMeasurementsEvent.Callback} object to register. - * @param handler the handler that the callback runs on. + * @param handler the handler that the callback runs on. * @return {@code true} if the callback was added successfully, {@code false} otherwise. */ @RequiresPermission(ACCESS_FINE_LOCATION) public boolean registerGnssMeasurementsCallback( @NonNull GnssMeasurementsEvent.Callback callback, @Nullable Handler handler) { - return mGnssMeasurementCallbackTransport.add(callback, handler); + if (handler == null) { + handler = new Handler(); + } + try { + return mGnssMeasurementsListenerManager.addListener(callback, handler); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } } /** - * Injects GNSS measurement corrections into the GNSS chipset. + * Registers a GPS Measurement callback. * - * @param measurementCorrections a {@link GnssMeasurementCorrections} object with the GNSS - * measurement corrections to be injected into the GNSS chipset. - * @hide + * @param callback a {@link GnssMeasurementsEvent.Callback} object to register. + * @param executor the executor that the callback runs on. + * @return {@code true} if the callback was added successfully, {@code false} otherwise. */ - @SystemApi @RequiresPermission(ACCESS_FINE_LOCATION) - public void injectGnssMeasurementCorrections( - @NonNull GnssMeasurementCorrections measurementCorrections) { + public boolean registerGnssMeasurementsCallback( + @NonNull @CallbackExecutor Executor executor, + @NonNull GnssMeasurementsEvent.Callback callback) { try { - mGnssMeasurementCallbackTransport.injectGnssMeasurementCorrections( - measurementCorrections); + return mGnssMeasurementsListenerManager.addListener(callback, executor); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Returns the supported capabilities of the GNSS chipset. - * - * @throws SecurityException if the ACCESS_FINE_LOCATION permission is not present. + * Injects GNSS measurement corrections into the GNSS chipset. * + * @param measurementCorrections a {@link GnssMeasurementCorrections} object with the GNSS + * measurement corrections to be injected into the GNSS chipset. * @hide */ @SystemApi @RequiresPermission(ACCESS_FINE_LOCATION) - public @NonNull GnssCapabilities getGnssCapabilities() { + public void injectGnssMeasurementCorrections( + @NonNull GnssMeasurementCorrections measurementCorrections) { + Preconditions.checkArgument(measurementCorrections != null); try { - long gnssCapabilities = mGnssMeasurementCallbackTransport.getGnssCapabilities(); - if (gnssCapabilities == GnssCapabilities.INVALID_CAPABILITIES) { - gnssCapabilities = 0L; - } - return GnssCapabilities.of(gnssCapabilities); + mService.injectGnssMeasurementCorrections( + measurementCorrections, mContext.getPackageName()); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * No-op method to keep backward-compatibility. Don't use it. Use {@link - * #unregisterGnssMeasurementsCallback} instead. - * - * @hide - * @deprecated use {@link #unregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback)} - * instead. - * @removed - */ - @Deprecated - @SystemApi - @SuppressLint("Doclava125") - public void removeGpsMeasurementListener(GpsMeasurementsEvent.Listener listener) {} - - /** * Unregisters a GPS Measurement callback. * * @param callback a {@link GnssMeasurementsEvent.Callback} object to remove. */ public void unregisterGnssMeasurementsCallback( @NonNull GnssMeasurementsEvent.Callback callback) { - mGnssMeasurementCallbackTransport.remove(callback); + try { + mGnssMeasurementsListenerManager.removeListener(callback); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } } /** @@ -2063,7 +2098,11 @@ public class LocationManager { * * @param callback a {@link GnssNavigationMessage.Callback} object to register. * @return {@code true} if the callback was added successfully, {@code false} otherwise. + * @deprecated Use {@link + * #registerGnssNavigationMessageCallback(GnssNavigationMessage.Callback, Handler)} or {@link + * #registerGnssNavigationMessageCallback(Executor, GnssNavigationMessage.Callback)} instead. */ + @Deprecated public boolean registerGnssNavigationMessageCallback( @NonNull GnssNavigationMessage.Callback callback) { return registerGnssNavigationMessageCallback(callback, null); @@ -2073,78 +2112,50 @@ public class LocationManager { * Registers a GNSS Navigation Message callback. * * @param callback a {@link GnssNavigationMessage.Callback} object to register. - * @param handler the handler that the callback runs on. + * @param handler the handler that the callback runs on. * @return {@code true} if the callback was added successfully, {@code false} otherwise. */ @RequiresPermission(ACCESS_FINE_LOCATION) public boolean registerGnssNavigationMessageCallback( @NonNull GnssNavigationMessage.Callback callback, @Nullable Handler handler) { - return mGnssNavigationMessageCallbackTransport.add(callback, handler); - } - - /** - * Unregisters a GNSS Navigation Message callback. - * - * @param callback a {@link GnssNavigationMessage.Callback} object to remove. - */ - public void unregisterGnssNavigationMessageCallback( - @NonNull GnssNavigationMessage.Callback callback) { - mGnssNavigationMessageCallbackTransport.remove(callback); - } - - /** - * Retrieves information about the current status of the GPS engine. - * This should only be called from the {@link GpsStatus.Listener#onGpsStatusChanged} - * callback to ensure that the data is copied atomically. - * - * The caller may either pass in a {@link GpsStatus} object to set with the latest - * status information, or pass null to create a new {@link GpsStatus} object. - * - * @param status object containing GPS status details, or null. - * @return status object containing updated GPS status. - */ - @Deprecated - @RequiresPermission(ACCESS_FINE_LOCATION) - public @Nullable GpsStatus getGpsStatus(@Nullable GpsStatus status) { - if (status == null) { - status = new GpsStatus(); + if (handler == null) { + handler = new Handler(); } - // When mGnssStatus is null, that means that this method is called outside - // onGpsStatusChanged(). Return an empty status to maintain backwards compatibility. - if (mGnssStatus != null) { - status.setStatus(mGnssStatus, mTimeToFirstFix); + + try { + return mGnssNavigationMessageListenerTransport.addListener(callback, handler); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); } - return status; } /** - * Returns the model year of the GNSS hardware and software build. - * - * <p> More details, such as build date, may be available in {@link #getGnssHardwareModelName()}. + * Registers a GNSS Navigation Message callback. * - * <p> May return 0 if the model year is less than 2016. + * @param callback a {@link GnssNavigationMessage.Callback} object to register. + * @param executor the looper that the callback runs on. + * @return {@code true} if the callback was added successfully, {@code false} otherwise. */ - public int getGnssYearOfHardware() { + @RequiresPermission(ACCESS_FINE_LOCATION) + public boolean registerGnssNavigationMessageCallback( + @NonNull @CallbackExecutor Executor executor, + @NonNull GnssNavigationMessage.Callback callback) { try { - return mService.getGnssYearOfHardware(); + return mGnssNavigationMessageListenerTransport.addListener(callback, executor); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } } /** - * Returns the Model Name (including Vendor and Hardware/Software Version) of the GNSS hardware - * driver. - * - * <p> No device-specific serial number or ID is returned from this API. + * Unregisters a GNSS Navigation Message callback. * - * <p> Will return null when the GNSS hardware abstraction layer does not support providing - * this value. + * @param callback a {@link GnssNavigationMessage.Callback} object to remove. */ - @Nullable - public String getGnssHardwareModelName() { + public void unregisterGnssNavigationMessageCallback( + @NonNull GnssNavigationMessage.Callback callback) { try { - return mService.getGnssHardwareModelName(); + mGnssNavigationMessageListenerTransport.removeListener(callback); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } @@ -2192,12 +2203,20 @@ public class LocationManager { @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) public boolean registerGnssBatchedLocationCallback(long periodNanos, boolean wakeOnFifoFull, @NonNull BatchedLocationCallback callback, @Nullable Handler handler) { - mBatchedLocationCallbackTransport.add(callback, handler); + if (handler == null) { + handler = new Handler(); + } - try { - return mService.startGnssBatch(periodNanos, wakeOnFifoFull, mContext.getPackageName()); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); + synchronized (mBatchedLocationCallbackManager) { + try { + if (mBatchedLocationCallbackManager.addListener(callback, handler)) { + return mService.startGnssBatch(periodNanos, wakeOnFifoFull, + mContext.getPackageName()); + } + return false; + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } } } @@ -2231,34 +2250,14 @@ public class LocationManager { @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) public boolean unregisterGnssBatchedLocationCallback( @NonNull BatchedLocationCallback callback) { - - mBatchedLocationCallbackTransport.remove(callback); - - try { - return mService.stopGnssBatch(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - } - - /** - * Sends additional commands to a location provider. Can be used to support provider specific - * extensions to the Location Manager API. - * - * @param provider name of the location provider. - * @param command name of the command to send to the provider. - * @param extras optional arguments for the command (or null). - * @return true always - */ - public boolean sendExtraCommand( - @NonNull String provider, @NonNull String command, @Nullable Bundle extras) { - Preconditions.checkArgument(provider != null, "invalid null provider"); - Preconditions.checkArgument(command != null, "invalid null command"); - - try { - return mService.sendExtraCommand(provider, command, extras); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); + synchronized (mBatchedLocationCallbackManager) { + try { + mBatchedLocationCallbackManager.removeListener(callback); + mService.stopGnssBatch(); + return true; + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } } } @@ -2316,117 +2315,391 @@ public class LocationManager { } } - /** - * Returns true if the given package name matches a location provider package, and false - * otherwise. - * - * @hide - */ - @SystemApi - @RequiresPermission(Manifest.permission.READ_DEVICE_CONFIG) - public boolean isProviderPackage(@NonNull String packageName) { - try { - return mService.isProviderPackage(packageName); - } catch (RemoteException e) { - e.rethrowFromSystemServer(); - return false; + private class LocationListenerTransport extends ILocationListener.Stub { + + private final Executor mExecutor; + @Nullable private volatile LocationListener mListener; + + private LocationListenerTransport(@NonNull Handler handler, + @NonNull LocationListener listener) { + Preconditions.checkArgument(handler != null, "invalid null handler"); + Preconditions.checkArgument(listener != null, "invalid null listener"); + + mExecutor = new HandlerExecutor(handler); + mListener = listener; } - } - /** - * Set the extra location controller package for location services on the device. - * - * @hide - */ - @SystemApi - @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) - public void setExtraLocationControllerPackage(@Nullable String packageName) { - try { - mService.setExtraLocationControllerPackage(packageName); - } catch (RemoteException e) { - e.rethrowFromSystemServer(); + private LocationListenerTransport(@NonNull Executor executor, + @NonNull LocationListener listener) { + Preconditions.checkArgument(executor != null, "invalid null executor"); + Preconditions.checkArgument(listener != null, "invalid null listener"); + + mExecutor = executor; + mListener = listener; } - } - /** - * Set the extra location controller package for location services on the device. - * - * @removed - * @deprecated Use {@link #setExtraLocationControllerPackage} instead. - * @hide - */ - @Deprecated - @SystemApi - @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) - public void setLocationControllerExtraPackage(String packageName) { - try { - mService.setExtraLocationControllerPackage(packageName); - } catch (RemoteException e) { - e.rethrowFromSystemServer(); + private LocationListener getKey() { + return mListener; + } + + private void unregisterListener() { + mListener = null; + } + + @Override + public void onLocationChanged(Location location) { + try { + mExecutor.execute(() -> { + try { + LocationListener listener = mListener; + if (listener == null) { + return; + } + + // we may be under the binder identity if a direct executor is used + long identity = Binder.clearCallingIdentity(); + try { + listener.onLocationChanged(location); + } finally { + Binder.restoreCallingIdentity(identity); + } + } finally { + locationCallbackFinished(); + } + }); + } catch (RejectedExecutionException e) { + locationCallbackFinished(); + throw e; + } + } + + @Override + public void onStatusChanged(String provider, int status, Bundle extras) { + try { + mExecutor.execute(() -> { + try { + LocationListener listener = mListener; + if (listener == null) { + return; + } + + // we may be under the binder identity if a direct executor is used + long identity = Binder.clearCallingIdentity(); + try { + listener.onStatusChanged(provider, status, extras); + } finally { + Binder.restoreCallingIdentity(identity); + } + } finally { + locationCallbackFinished(); + } + }); + } catch (RejectedExecutionException e) { + locationCallbackFinished(); + throw e; + } + } + + @Override + public void onProviderEnabled(String provider) { + try { + mExecutor.execute(() -> { + try { + LocationListener listener = mListener; + if (listener == null) { + return; + } + + // we may be under the binder identity if a direct executor is used + long identity = Binder.clearCallingIdentity(); + try { + listener.onProviderEnabled(provider); + } finally { + Binder.restoreCallingIdentity(identity); + } + } finally { + locationCallbackFinished(); + } + }); + } catch (RejectedExecutionException e) { + locationCallbackFinished(); + throw e; + } + } + + @Override + public void onProviderDisabled(String provider) { + try { + mExecutor.execute(() -> { + try { + LocationListener listener = mListener; + if (listener == null) { + return; + } + + // we may be under the binder identity if a direct executor is used + long identity = Binder.clearCallingIdentity(); + try { + listener.onProviderDisabled(provider); + } finally { + Binder.restoreCallingIdentity(identity); + } + } finally { + locationCallbackFinished(); + } + }); + } catch (RejectedExecutionException e) { + locationCallbackFinished(); + throw e; + } + } + + private void locationCallbackFinished() { + try { + mService.locationCallbackFinished(this); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } } } - /** - * Returns the extra location controller package on the device. - * - * @hide - */ - @SystemApi - public @Nullable String getExtraLocationControllerPackage() { - try { - return mService.getExtraLocationControllerPackage(); - } catch (RemoteException e) { - e.rethrowFromSystemServer(); - return null; + private static class NmeaAdapter extends GnssStatus.Callback implements OnNmeaMessageListener { + + private final OnNmeaMessageListener mListener; + + private NmeaAdapter(OnNmeaMessageListener listener) { + mListener = listener; + } + + @Override + public void onNmeaMessage(String message, long timestamp) { + mListener.onNmeaMessage(message, timestamp); } } - /** - * Set whether the extra location controller package is currently enabled on the device. - * - * @removed - * @deprecated Use {@link #setExtraLocationControllerPackageEnabled} instead. - * @hide - */ - @SystemApi - @Deprecated - @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) - public void setLocationControllerExtraPackageEnabled(boolean enabled) { - try { - mService.setExtraLocationControllerPackageEnabled(enabled); - } catch (RemoteException e) { - e.rethrowFromSystemServer(); + private class GnssStatusListenerManager extends + AbstractListenerManager<GnssStatus.Callback> { + + @Nullable + private IGnssStatusListener mListenerTransport; + + @Nullable + private volatile GnssStatus mGnssStatus; + private volatile int mTtff; + + public GnssStatus getGnssStatus() { + return mGnssStatus; + } + + public int getTtff() { + return mTtff; + } + + public boolean addListener(@NonNull GpsStatus.Listener listener, @NonNull Handler handler) + throws RemoteException { + return addInternal(listener, handler); + } + + public boolean addListener(@NonNull OnNmeaMessageListener listener, + @NonNull Handler handler) + throws RemoteException { + return addInternal(listener, handler); + } + + public boolean addListener(@NonNull OnNmeaMessageListener listener, + @NonNull Executor executor) + throws RemoteException { + return addInternal(listener, executor); + } + + @Override + protected GnssStatus.Callback convertKey(Object listener) { + if (listener instanceof GnssStatus.Callback) { + return (GnssStatus.Callback) listener; + } else if (listener instanceof GpsStatus.Listener) { + return new GnssStatus.Callback() { + private final GpsStatus.Listener mGpsListener = (GpsStatus.Listener) listener; + + @Override + public void onStarted() { + mGpsListener.onGpsStatusChanged(GpsStatus.GPS_EVENT_STARTED); + } + + @Override + public void onStopped() { + mGpsListener.onGpsStatusChanged(GpsStatus.GPS_EVENT_STOPPED); + } + + @Override + public void onFirstFix(int ttffMillis) { + mGpsListener.onGpsStatusChanged(GpsStatus.GPS_EVENT_FIRST_FIX); + } + + @Override + public void onSatelliteStatusChanged(GnssStatus status) { + mGpsListener.onGpsStatusChanged(GpsStatus.GPS_EVENT_SATELLITE_STATUS); + } + }; + } else if (listener instanceof OnNmeaMessageListener) { + return new NmeaAdapter((OnNmeaMessageListener) listener); + } else { + throw new IllegalStateException(); + } + } + + @Override + protected boolean registerService() throws RemoteException { + Preconditions.checkState(mListenerTransport == null); + + mListenerTransport = new GnssStatusListener(); + return mService.registerGnssStatusCallback(mListenerTransport, + mContext.getPackageName()); + } + + @Override + protected void unregisterService() throws RemoteException { + Preconditions.checkState(mListenerTransport != null); + + mService.unregisterGnssStatusCallback(mListenerTransport); + mListenerTransport = null; + } + + private class GnssStatusListener extends IGnssStatusListener.Stub { + @Override + public void onGnssStarted() { + execute(GnssStatus.Callback::onStarted); + } + + @Override + public void onGnssStopped() { + execute(GnssStatus.Callback::onStopped); + } + + @Override + public void onFirstFix(int ttff) { + mTtff = ttff; + execute((callback) -> callback.onFirstFix(ttff)); + } + + @Override + public void onSvStatusChanged(int svCount, int[] svidWithFlags, float[] cn0s, + float[] elevations, float[] azimuths, float[] carrierFreqs) { + GnssStatus localStatus = new GnssStatus(svCount, svidWithFlags, cn0s, elevations, + azimuths, carrierFreqs); + mGnssStatus = localStatus; + execute((callback) -> callback.onSatelliteStatusChanged(localStatus)); + } + + @Override + public void onNmeaReceived(long timestamp, String nmea) { + execute((callback) -> { + if (callback instanceof NmeaAdapter) { + ((NmeaAdapter) callback).onNmeaMessage(nmea, timestamp); + } + }); + } } } - /** - * Set whether the extra location controller package is currently enabled on the device. - * - * @hide - */ - @SystemApi - @RequiresPermission(Manifest.permission.LOCATION_HARDWARE) - public void setExtraLocationControllerPackageEnabled(boolean enabled) { - try { - mService.setExtraLocationControllerPackageEnabled(enabled); - } catch (RemoteException e) { - e.rethrowFromSystemServer(); + private class GnssMeasurementsListenerManager extends + AbstractListenerManager<GnssMeasurementsEvent.Callback> { + + @Nullable + private IGnssMeasurementsListener mListenerTransport; + + @Override + protected boolean registerService() throws RemoteException { + Preconditions.checkState(mListenerTransport == null); + + mListenerTransport = new GnssMeasurementsListener(); + return mService.addGnssMeasurementsListener(mListenerTransport, + mContext.getPackageName()); + } + + @Override + protected void unregisterService() throws RemoteException { + Preconditions.checkState(mListenerTransport != null); + + mService.removeGnssMeasurementsListener(mListenerTransport); + mListenerTransport = null; + } + + private class GnssMeasurementsListener extends IGnssMeasurementsListener.Stub { + @Override + public void onGnssMeasurementsReceived(final GnssMeasurementsEvent event) { + execute((callback) -> callback.onGnssMeasurementsReceived(event)); + } + + @Override + public void onStatusChanged(int status) { + execute((callback) -> callback.onStatusChanged(status)); + } } } - /** - * Returns whether extra location controller package is currently enabled on the device. - * - * @hide - */ - @SystemApi - public boolean isExtraLocationControllerPackageEnabled() { - try { - return mService.isExtraLocationControllerPackageEnabled(); - } catch (RemoteException e) { - e.rethrowFromSystemServer(); - return false; + private class GnssNavigationMessageListenerManager extends + AbstractListenerManager<GnssNavigationMessage.Callback> { + + @Nullable + private IGnssNavigationMessageListener mListenerTransport; + + @Override + protected boolean registerService() throws RemoteException { + Preconditions.checkState(mListenerTransport == null); + + mListenerTransport = new GnssNavigationMessageListener(); + return mService.addGnssNavigationMessageListener(mListenerTransport, + mContext.getPackageName()); + } + + @Override + protected void unregisterService() throws RemoteException { + Preconditions.checkState(mListenerTransport != null); + + mService.removeGnssNavigationMessageListener(mListenerTransport); + mListenerTransport = null; + } + + private class GnssNavigationMessageListener extends IGnssNavigationMessageListener.Stub { + @Override + public void onGnssNavigationMessageReceived(GnssNavigationMessage event) { + execute((listener) -> listener.onGnssNavigationMessageReceived(event)); + } + + @Override + public void onStatusChanged(int status) { + execute((listener) -> listener.onStatusChanged(status)); + } } } + private class BatchedLocationCallbackManager extends + AbstractListenerManager<BatchedLocationCallback> { + + @Nullable + private IBatchedLocationCallback mListenerTransport; + + @Override + protected boolean registerService() throws RemoteException { + Preconditions.checkState(mListenerTransport == null); + + mListenerTransport = new BatchedLocationCallback(); + return mService.addGnssBatchingCallback(mListenerTransport, mContext.getPackageName()); + } + + @Override + protected void unregisterService() throws RemoteException { + Preconditions.checkState(mListenerTransport != null); + + mService.removeGnssBatchingCallback(); + mListenerTransport = null; + } + + private class BatchedLocationCallback extends IBatchedLocationCallback.Stub { + @Override + public void onLocationBatch(List<Location> locations) { + execute((listener) -> listener.onLocationBatch(locations)); + } + } + } } diff --git a/media/java/android/media/ExifInterface.java b/media/java/android/media/ExifInterface.java index 5b535651abd9..53babcb32e4d 100644 --- a/media/java/android/media/ExifInterface.java +++ b/media/java/android/media/ExifInterface.java @@ -494,6 +494,19 @@ public class ExifInterface { // See http://www.exiv2.org/makernote.html#R11 private static final int PEF_MAKER_NOTE_SKIP_SIZE = 6; + // See PNG (Portable Network Graphics) Specification, Version 1.2, + // 3.1. PNG file signature + private static final byte[] PNG_SIGNATURE = new byte[] {(byte) 0x89, (byte) 0x50, (byte) 0x4e, + (byte) 0x47, (byte) 0x0d, (byte) 0x0a, (byte) 0x1a, (byte) 0x0a}; + // See PNG (Portable Network Graphics) Specification, Version 1.2, + // 3.7. eXIf Exchangeable Image File (Exif) Profile + private static final byte[] PNG_CHUNK_TYPE_EXIF = new byte[]{(byte) 0x65, (byte) 0x58, + (byte) 0x49, (byte) 0x66}; + private static final byte[] PNG_CHUNK_TYPE_IEND = new byte[]{(byte) 0x49, (byte) 0x45, + (byte) 0x4e, (byte) 0x44}; + private static final int PNG_CHUNK_LENGTH_BYTE_LENGTH = 4; + private static final int PNG_CHUNK_CRC_BYTE_LENGTH = 4; + @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) private static SimpleDateFormat sFormatter; private static SimpleDateFormat sFormatterTz; @@ -1311,6 +1324,7 @@ public class ExifInterface { private static final int IMAGE_TYPE_RW2 = 10; private static final int IMAGE_TYPE_SRW = 11; private static final int IMAGE_TYPE_HEIF = 12; + private static final int IMAGE_TYPE_PNG = 13; static { sFormatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss"); @@ -1811,6 +1825,10 @@ public class ExifInterface { getRw2Attributes(inputStream); break; } + case IMAGE_TYPE_PNG: { + getPngAttributes(inputStream); + break; + } case IMAGE_TYPE_ARW: case IMAGE_TYPE_CR2: case IMAGE_TYPE_DNG: @@ -2024,6 +2042,7 @@ public class ExifInterface { if (in.skip(mThumbnailOffset) != mThumbnailOffset) { throw new IOException("Corrupted image"); } + // TODO: Need to handle potential OutOfMemoryError byte[] buffer = new byte[mThumbnailLength]; if (in.read(buffer) != mThumbnailLength) { throw new IOException("Corrupted image"); @@ -2363,6 +2382,8 @@ public class ExifInterface { return IMAGE_TYPE_ORF; } else if (isRw2Format(signatureCheckBytes)) { return IMAGE_TYPE_RW2; + } else if (isPngFormat(signatureCheckBytes)) { + return IMAGE_TYPE_PNG; } // Certain file formats (PEF) are identified in readImageFileDirectory() return IMAGE_TYPE_UNKNOWN; @@ -2478,16 +2499,24 @@ public class ExifInterface { * http://fileformats.archiveteam.org/wiki/Olympus_ORF */ private boolean isOrfFormat(byte[] signatureCheckBytes) throws IOException { - ByteOrderedDataInputStream signatureInputStream = - new ByteOrderedDataInputStream(signatureCheckBytes); - // Read byte order - mExifByteOrder = readByteOrder(signatureInputStream); - // Set byte order - signatureInputStream.setByteOrder(mExifByteOrder); + ByteOrderedDataInputStream signatureInputStream = null; - short orfSignature = signatureInputStream.readShort(); - if (orfSignature == ORF_SIGNATURE_1 || orfSignature == ORF_SIGNATURE_2) { - return true; + try { + signatureInputStream = new ByteOrderedDataInputStream(signatureCheckBytes); + + // Read byte order + mExifByteOrder = readByteOrder(signatureInputStream); + // Set byte order + signatureInputStream.setByteOrder(mExifByteOrder); + + short orfSignature = signatureInputStream.readShort(); + return orfSignature == ORF_SIGNATURE_1 || orfSignature == ORF_SIGNATURE_2; + } catch (Exception e) { + // Do nothing + } finally { + if (signatureInputStream != null) { + signatureInputStream.close(); + } } return false; } @@ -2497,21 +2526,43 @@ public class ExifInterface { * See http://lclevy.free.fr/raw/ */ private boolean isRw2Format(byte[] signatureCheckBytes) throws IOException { - ByteOrderedDataInputStream signatureInputStream = - new ByteOrderedDataInputStream(signatureCheckBytes); - // Read byte order - mExifByteOrder = readByteOrder(signatureInputStream); - // Set byte order - signatureInputStream.setByteOrder(mExifByteOrder); + ByteOrderedDataInputStream signatureInputStream = null; - short signatureByte = signatureInputStream.readShort(); - if (signatureByte == RW2_SIGNATURE) { - return true; + try { + signatureInputStream = new ByteOrderedDataInputStream(signatureCheckBytes); + + // Read byte order + mExifByteOrder = readByteOrder(signatureInputStream); + // Set byte order + signatureInputStream.setByteOrder(mExifByteOrder); + + short signatureByte = signatureInputStream.readShort(); + signatureInputStream.close(); + return signatureByte == RW2_SIGNATURE; + } catch (Exception e) { + // Do nothing + } finally { + if (signatureInputStream != null) { + signatureInputStream.close(); + } } return false; } /** + * PNG's file signature is first 8 bytes. + * See PNG (Portable Network Graphics) Specification, Version 1.2, 3.1. PNG file signature + */ + private boolean isPngFormat(byte[] signatureCheckBytes) throws IOException { + for (int i = 0; i < PNG_SIGNATURE.length; i++) { + if (signatureCheckBytes[i] != PNG_SIGNATURE[i]) { + return false; + } + } + return true; + } + + /** * Loads EXIF attributes from a JPEG input stream. * * @param in The input stream that starts with the JPEG data. @@ -2585,7 +2636,7 @@ public class ExifInterface { readExifSegment(value, imageType); - // Save offset values for createJpegThumbnailBitmap() function + // Save offset values for handleThumbnailFromJfif() function mExifOffset = (int) offset; } else if (ArrayUtils.startsWith(bytes, IDENTIFIER_XMP_APP1)) { // See XMP Specification Part 3: Storage in Files, 1.1.3 JPEG, Table 6 @@ -2886,6 +2937,7 @@ public class ExifInterface { throw new IOException("Invalid identifier"); } + // TODO: Need to handle potential OutOfMemoryError byte[] bytes = new byte[length]; if (in.read(bytes) != length) { throw new IOException("Can't read exif"); @@ -3012,6 +3064,64 @@ public class ExifInterface { } } + // PNG contains the EXIF data as a Special-Purpose Chunk + private void getPngAttributes(ByteOrderedDataInputStream in) throws IOException { + if (DEBUG) { + Log.d(TAG, "getPngAttributes starting with: " + in); + } + + // PNG uses Big Endian by default. + // See PNG (Portable Network Graphics) Specification, Version 1.2, + // 2.1. Integers and byte order + in.setByteOrder(ByteOrder.BIG_ENDIAN); + + // Skip the signature bytes + in.seek(PNG_SIGNATURE.length); + + try { + while (true) { + // Each chunk is made up of four parts: + // 1) Length: 4-byte unsigned integer indicating the number of bytes in the + // Chunk Data field. Excludes Chunk Type and CRC bytes. + // 2) Chunk Type: 4-byte chunk type code. + // 3) Chunk Data: The data bytes. Can be zero-length. + // 4) CRC: 4-byte data calculated on the preceding bytes in the chunk. Always + // present. + // --> 4 (length bytes) + 4 (type bytes) + X (data bytes) + 4 (CRC bytes) + // See PNG (Portable Network Graphics) Specification, Version 1.2, + // 3.2. Chunk layout + int length = in.readInt(); + + byte[] type = new byte[PNG_CHUNK_LENGTH_BYTE_LENGTH]; + if (in.read(type) != type.length) { + throw new IOException("Encountered invalid length while parsing PNG chunk" + + "type"); + } + + if (Arrays.equals(type, PNG_CHUNK_TYPE_IEND)) { + // IEND marks the end of the image. + break; + } else if (Arrays.equals(type, PNG_CHUNK_TYPE_EXIF)) { + // TODO: Need to handle potential OutOfMemoryError + byte[] data = new byte[length]; + if (in.read(data) != length) { + throw new IOException("Failed to read given length for given PNG chunk " + + "type: " + byteArrayToHexString(type)); + } + readExifSegment(data, IFD_TYPE_PRIMARY); + break; + } else { + // Skip to next chunk + in.skipBytes(length + PNG_CHUNK_CRC_BYTE_LENGTH); + } + } + } catch (EOFException e) { + // Should not reach here. Will only reach here if the file is corrupted or + // does not follow the PNG specifications + throw new IOException("Encountered corrupt PNG file."); + } + } + // Stores a new JPEG image with EXIF attributes into a given output stream. private void saveJpegAttributes(InputStream inputStream, OutputStream outputStream) throws IOException { @@ -3517,6 +3627,7 @@ public class ExifInterface { if (mFilename == null && mAssetInputStream == null && mSeekableFileDescriptor == null) { + // TODO: Need to handle potential OutOfMemoryError // Save the thumbnail in memory if the input doesn't support reading again. byte[] thumbnailBytes = new byte[thumbnailLength]; in.seek(thumbnailOffset); @@ -3550,6 +3661,7 @@ public class ExifInterface { return; } + // TODO: Need to handle potential OutOfMemoryError // Set thumbnail byte array data for non-consecutive strip bytes byte[] totalStripBytes = new byte[(int) Arrays.stream(stripByteCounts).sum()]; @@ -3568,6 +3680,7 @@ public class ExifInterface { in.seek(skipBytes); bytesRead += skipBytes; + // TODO: Need to handle potential OutOfMemoryError // Read strip bytes byte[] stripBytes = new byte[stripByteCount]; in.read(stripBytes); @@ -4367,4 +4480,12 @@ public class ExifInterface { } return null; } + + private static String byteArrayToHexString(byte[] bytes) { + StringBuilder sb = new StringBuilder(bytes.length * 2); + for (int i = 0; i < bytes.length; i++) { + sb.append(String.format("%02x", bytes[i])); + } + return sb.toString(); + } } diff --git a/packages/BackupEncryption/src/com/android/server/backup/encryption/chunking/ProtoStore.java b/packages/BackupEncryption/src/com/android/server/backup/encryption/chunking/ProtoStore.java new file mode 100644 index 000000000000..3ba5f2b741b8 --- /dev/null +++ b/packages/BackupEncryption/src/com/android/server/backup/encryption/chunking/ProtoStore.java @@ -0,0 +1,174 @@ +/* + * Copyright (C) 2019 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.server.backup.encryption.chunking; + +import static com.android.internal.util.Preconditions.checkNotNull; + +import android.content.Context; +import android.text.TextUtils; +import android.util.AtomicFile; +import android.util.Slog; + +import com.android.internal.annotations.VisibleForTesting; +import com.android.server.backup.encryption.protos.nano.ChunksMetadataProto; +import com.android.server.backup.encryption.protos.nano.KeyValueListingProto; + +import com.google.protobuf.nano.MessageNano; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.Optional; + +/** + * Stores a nano proto for each package, persisting the proto to disk. + * + * <p>This is used to store {@link ChunksMetadataProto.ChunkListing}. + * + * @param <T> the type of nano proto to store. + */ +public class ProtoStore<T extends MessageNano> { + private static final String CHUNK_LISTING_FOLDER = "backup_chunk_listings"; + private static final String KEY_VALUE_LISTING_FOLDER = "backup_kv_listings"; + + private static final String TAG = "BupEncProtoStore"; + + private final File mStoreFolder; + private final Class<T> mClazz; + + /** Creates a new instance which stores chunk listings at the default location. */ + public static ProtoStore<ChunksMetadataProto.ChunkListing> createChunkListingStore( + Context context) throws IOException { + return new ProtoStore<>( + ChunksMetadataProto.ChunkListing.class, + new File(context.getFilesDir().getAbsoluteFile(), CHUNK_LISTING_FOLDER)); + } + + /** Creates a new instance which stores key value listings in the default location. */ + public static ProtoStore<KeyValueListingProto.KeyValueListing> createKeyValueListingStore( + Context context) throws IOException { + return new ProtoStore<>( + KeyValueListingProto.KeyValueListing.class, + new File(context.getFilesDir().getAbsoluteFile(), KEY_VALUE_LISTING_FOLDER)); + } + + /** + * Creates a new instance which stores protos in the given folder. + * + * @param storeFolder The location where the serialized form is stored. + */ + @VisibleForTesting + ProtoStore(Class<T> clazz, File storeFolder) throws IOException { + mClazz = checkNotNull(clazz); + mStoreFolder = ensureDirectoryExistsOrThrow(storeFolder); + } + + private static File ensureDirectoryExistsOrThrow(File directory) throws IOException { + if (directory.exists() && !directory.isDirectory()) { + throw new IOException("Store folder already exists, but isn't a directory."); + } + + if (!directory.exists() && !directory.mkdir()) { + throw new IOException("Unable to create store folder."); + } + + return directory; + } + + /** + * Returns the chunk listing for the given package, or {@link Optional#empty()} if no listing + * exists. + */ + public Optional<T> loadProto(String packageName) + throws IOException, IllegalAccessException, InstantiationException, + NoSuchMethodException, InvocationTargetException { + File file = getFileForPackage(packageName); + + if (!file.exists()) { + Slog.d( + TAG, + "No chunk listing existed for " + packageName + ", returning empty listing."); + return Optional.empty(); + } + + AtomicFile protoStore = new AtomicFile(file); + byte[] data = protoStore.readFully(); + + Constructor<T> constructor = mClazz.getDeclaredConstructor(); + T proto = constructor.newInstance(); + MessageNano.mergeFrom(proto, data); + return Optional.of(proto); + } + + /** Saves a proto to disk, associating it with the given package. */ + public void saveProto(String packageName, T proto) throws IOException { + checkNotNull(proto); + File file = getFileForPackage(packageName); + + try (FileOutputStream os = new FileOutputStream(file)) { + os.write(MessageNano.toByteArray(proto)); + } catch (IOException e) { + Slog.e( + TAG, + "Exception occurred when saving the listing for " + + packageName + + ", deleting saved listing.", + e); + + // If a problem occurred when writing the listing then it might be corrupt, so delete + // it. + file.delete(); + + throw e; + } + } + + /** Deletes the proto for the given package, or does nothing if the package has no proto. */ + public void deleteProto(String packageName) { + File file = getFileForPackage(packageName); + file.delete(); + } + + /** Deletes every proto of this type, for all package names. */ + public void deleteAllProtos() { + File[] files = mStoreFolder.listFiles(); + + // We ensure that the storeFolder exists in the constructor, but check just in case it has + // mysteriously disappeared. + if (files == null) { + return; + } + + for (File file : files) { + file.delete(); + } + } + + private File getFileForPackage(String packageName) { + checkPackageName(packageName); + return new File(mStoreFolder, packageName); + } + + private static void checkPackageName(String packageName) { + if (TextUtils.isEmpty(packageName) || packageName.contains("/")) { + throw new IllegalArgumentException( + "Package name must not contain '/' or be empty: " + packageName); + } + } +} diff --git a/packages/BackupEncryption/src/com/android/server/backup/encryption/transport/IntermediateEncryptingTransport.java b/packages/BackupEncryption/src/com/android/server/backup/encryption/transport/IntermediateEncryptingTransport.java index da47781d77d1..1d0224d49be7 100644 --- a/packages/BackupEncryption/src/com/android/server/backup/encryption/transport/IntermediateEncryptingTransport.java +++ b/packages/BackupEncryption/src/com/android/server/backup/encryption/transport/IntermediateEncryptingTransport.java @@ -16,7 +16,10 @@ package com.android.server.backup.encryption.transport; +import static com.android.server.backup.encryption.BackupEncryptionService.TAG; + import android.os.RemoteException; +import android.util.Log; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.backup.IBackupTransport; @@ -47,6 +50,7 @@ public class IntermediateEncryptingTransport extends DelegatingTransport { } private void connect() throws RemoteException { + Log.i(TAG, "connecting " + mTransportClient); synchronized (mConnectLock) { if (mRealTransport == null) { mRealTransport = mTransportClient.connect("IntermediateEncryptingTransport"); diff --git a/packages/BackupEncryption/src/com/android/server/backup/encryption/transport/IntermediateEncryptingTransportManager.java b/packages/BackupEncryption/src/com/android/server/backup/encryption/transport/IntermediateEncryptingTransportManager.java index 5a8b05c9f0fe..6e6d571aa3c7 100644 --- a/packages/BackupEncryption/src/com/android/server/backup/encryption/transport/IntermediateEncryptingTransportManager.java +++ b/packages/BackupEncryption/src/com/android/server/backup/encryption/transport/IntermediateEncryptingTransportManager.java @@ -72,6 +72,7 @@ public class IntermediateEncryptingTransportManager { * Create an instance of {@link IntermediateEncryptingTransport}. */ private IntermediateEncryptingTransport create(Intent realTransportIntent) { + Log.d(TAG, "create: intent:" + realTransportIntent); return new IntermediateEncryptingTransport(mTransportClientManager.getTransportClient( realTransportIntent.getComponent(), realTransportIntent.getExtras(), CALLER)); } diff --git a/packages/BackupEncryption/test/robolectric/src/com/android/server/backup/encryption/chunking/ProtoStoreTest.java b/packages/BackupEncryption/test/robolectric/src/com/android/server/backup/encryption/chunking/ProtoStoreTest.java new file mode 100644 index 000000000000..d73c8e47f609 --- /dev/null +++ b/packages/BackupEncryption/test/robolectric/src/com/android/server/backup/encryption/chunking/ProtoStoreTest.java @@ -0,0 +1,264 @@ +/* + * Copyright (C) 2019 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.server.backup.encryption.chunking; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; + +import static org.testng.Assert.assertThrows; + +import android.content.Context; +import android.platform.test.annotations.Presubmit; + +import androidx.test.core.app.ApplicationProvider; + +import com.android.server.backup.encryption.chunk.ChunkHash; +import com.android.server.backup.encryption.protos.nano.ChunksMetadataProto; +import com.android.server.backup.encryption.protos.nano.KeyValueListingProto; + +import com.google.common.collect.ImmutableMap; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map.Entry; +import java.util.Optional; + +@RunWith(RobolectricTestRunner.class) +@Presubmit +public class ProtoStoreTest { + private static final String TEST_KEY_1 = "test_key_1"; + private static final ChunkHash TEST_HASH_1 = + new ChunkHash(Arrays.copyOf(new byte[] {1}, EncryptedChunk.KEY_LENGTH_BYTES)); + private static final ChunkHash TEST_HASH_2 = + new ChunkHash(Arrays.copyOf(new byte[] {2}, EncryptedChunk.KEY_LENGTH_BYTES)); + private static final int TEST_LENGTH_1 = 10; + private static final int TEST_LENGTH_2 = 18; + + private static final String TEST_PACKAGE_1 = "com.example.test1"; + private static final String TEST_PACKAGE_2 = "com.example.test2"; + + @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder(); + + private File mStoreFolder; + private ProtoStore<ChunksMetadataProto.ChunkListing> mProtoStore; + + @Before + public void setUp() throws Exception { + mStoreFolder = mTemporaryFolder.newFolder(); + mProtoStore = new ProtoStore<>(ChunksMetadataProto.ChunkListing.class, mStoreFolder); + } + + @Test + public void differentStoreTypes_operateSimultaneouslyWithoutInterfering() throws Exception { + ChunksMetadataProto.ChunkListing chunkListing = + createChunkListing(ImmutableMap.of(TEST_HASH_1, TEST_LENGTH_1)); + KeyValueListingProto.KeyValueListing keyValueListing = + new KeyValueListingProto.KeyValueListing(); + keyValueListing.entries = new KeyValueListingProto.KeyValueEntry[1]; + keyValueListing.entries[0] = new KeyValueListingProto.KeyValueEntry(); + keyValueListing.entries[0].key = TEST_KEY_1; + keyValueListing.entries[0].hash = TEST_HASH_1.getHash(); + + Context application = ApplicationProvider.getApplicationContext(); + ProtoStore<ChunksMetadataProto.ChunkListing> chunkListingStore = + ProtoStore.createChunkListingStore(application); + ProtoStore<KeyValueListingProto.KeyValueListing> keyValueListingStore = + ProtoStore.createKeyValueListingStore(application); + + chunkListingStore.saveProto(TEST_PACKAGE_1, chunkListing); + keyValueListingStore.saveProto(TEST_PACKAGE_1, keyValueListing); + + ChunksMetadataProto.ChunkListing actualChunkListing = + chunkListingStore.loadProto(TEST_PACKAGE_1).get(); + KeyValueListingProto.KeyValueListing actualKeyValueListing = + keyValueListingStore.loadProto(TEST_PACKAGE_1).get(); + assertListingsEqual(actualChunkListing, chunkListing); + assertThat(actualKeyValueListing.entries.length).isEqualTo(1); + assertThat(actualKeyValueListing.entries[0].key).isEqualTo(TEST_KEY_1); + assertThat(actualKeyValueListing.entries[0].hash).isEqualTo(TEST_HASH_1.getHash()); + } + + @Test + public void construct_storeLocationIsFile_throws() throws Exception { + assertThrows( + IOException.class, + () -> + new ProtoStore<>( + ChunksMetadataProto.ChunkListing.class, + mTemporaryFolder.newFile())); + } + + @Test + public void loadChunkListing_noListingExists_returnsEmptyListing() throws Exception { + Optional<ChunksMetadataProto.ChunkListing> chunkListing = + mProtoStore.loadProto(TEST_PACKAGE_1); + assertThat(chunkListing.isPresent()).isFalse(); + } + + @Test + public void loadChunkListing_listingExists_returnsExistingListing() throws Exception { + ChunksMetadataProto.ChunkListing expected = + createChunkListing( + ImmutableMap.of(TEST_HASH_1, TEST_LENGTH_1, TEST_HASH_2, TEST_LENGTH_2)); + mProtoStore.saveProto(TEST_PACKAGE_1, expected); + + ChunksMetadataProto.ChunkListing result = mProtoStore.loadProto(TEST_PACKAGE_1).get(); + + assertListingsEqual(result, expected); + } + + @Test + public void loadProto_emptyPackageName_throwsException() throws Exception { + assertThrows(IllegalArgumentException.class, () -> mProtoStore.loadProto("")); + } + + @Test + public void loadProto_nullPackageName_throwsException() throws Exception { + assertThrows(IllegalArgumentException.class, () -> mProtoStore.loadProto(null)); + } + + @Test + public void loadProto_packageNameContainsSlash_throwsException() throws Exception { + assertThrows( + IllegalArgumentException.class, () -> mProtoStore.loadProto(TEST_PACKAGE_1 + "/")); + } + + @Test + public void saveProto_persistsToNewInstance() throws Exception { + ChunksMetadataProto.ChunkListing expected = + createChunkListing( + ImmutableMap.of(TEST_HASH_1, TEST_LENGTH_1, TEST_HASH_2, TEST_LENGTH_2)); + mProtoStore.saveProto(TEST_PACKAGE_1, expected); + mProtoStore = new ProtoStore<>(ChunksMetadataProto.ChunkListing.class, mStoreFolder); + + ChunksMetadataProto.ChunkListing result = mProtoStore.loadProto(TEST_PACKAGE_1).get(); + + assertListingsEqual(result, expected); + } + + @Test + public void saveProto_emptyPackageName_throwsException() throws Exception { + assertThrows( + IllegalArgumentException.class, + () -> mProtoStore.saveProto("", new ChunksMetadataProto.ChunkListing())); + } + + @Test + public void saveProto_nullPackageName_throwsException() throws Exception { + assertThrows( + IllegalArgumentException.class, + () -> mProtoStore.saveProto(null, new ChunksMetadataProto.ChunkListing())); + } + + @Test + public void saveProto_packageNameContainsSlash_throwsException() throws Exception { + assertThrows( + IllegalArgumentException.class, + () -> + mProtoStore.saveProto( + TEST_PACKAGE_1 + "/", new ChunksMetadataProto.ChunkListing())); + } + + @Test + public void saveProto_nullListing_throwsException() throws Exception { + assertThrows(NullPointerException.class, () -> mProtoStore.saveProto(TEST_PACKAGE_1, null)); + } + + @Test + public void deleteProto_noListingExists_doesNothing() throws Exception { + ChunksMetadataProto.ChunkListing listing = + createChunkListing(ImmutableMap.of(TEST_HASH_1, TEST_LENGTH_1)); + mProtoStore.saveProto(TEST_PACKAGE_1, listing); + + mProtoStore.deleteProto(TEST_PACKAGE_2); + + assertThat(mProtoStore.loadProto(TEST_PACKAGE_1).get().chunks.length).isEqualTo(1); + } + + @Test + public void deleteProto_listingExists_deletesListing() throws Exception { + ChunksMetadataProto.ChunkListing listing = + createChunkListing(ImmutableMap.of(TEST_HASH_1, TEST_LENGTH_1)); + mProtoStore.saveProto(TEST_PACKAGE_1, listing); + + mProtoStore.deleteProto(TEST_PACKAGE_1); + + assertThat(mProtoStore.loadProto(TEST_PACKAGE_1).isPresent()).isFalse(); + } + + @Test + public void deleteAllProtos_deletesAllProtos() throws Exception { + ChunksMetadataProto.ChunkListing listing1 = + createChunkListing(ImmutableMap.of(TEST_HASH_1, TEST_LENGTH_1)); + ChunksMetadataProto.ChunkListing listing2 = + createChunkListing(ImmutableMap.of(TEST_HASH_2, TEST_LENGTH_2)); + mProtoStore.saveProto(TEST_PACKAGE_1, listing1); + mProtoStore.saveProto(TEST_PACKAGE_2, listing2); + + mProtoStore.deleteAllProtos(); + + assertThat(mProtoStore.loadProto(TEST_PACKAGE_1).isPresent()).isFalse(); + assertThat(mProtoStore.loadProto(TEST_PACKAGE_2).isPresent()).isFalse(); + } + + @Test + public void deleteAllProtos_folderDeleted_doesNotCrash() throws Exception { + mStoreFolder.delete(); + + mProtoStore.deleteAllProtos(); + } + + private static ChunksMetadataProto.ChunkListing createChunkListing( + ImmutableMap<ChunkHash, Integer> chunks) { + ChunksMetadataProto.ChunkListing listing = new ChunksMetadataProto.ChunkListing(); + listing.cipherType = ChunksMetadataProto.AES_256_GCM; + listing.chunkOrderingType = ChunksMetadataProto.CHUNK_ORDERING_TYPE_UNSPECIFIED; + + List<ChunksMetadataProto.Chunk> chunkProtos = new ArrayList<>(); + for (Entry<ChunkHash, Integer> entry : chunks.entrySet()) { + ChunksMetadataProto.Chunk chunk = new ChunksMetadataProto.Chunk(); + chunk.hash = entry.getKey().getHash(); + chunk.length = entry.getValue(); + chunkProtos.add(chunk); + } + listing.chunks = chunkProtos.toArray(new ChunksMetadataProto.Chunk[0]); + return listing; + } + + private void assertListingsEqual( + ChunksMetadataProto.ChunkListing result, ChunksMetadataProto.ChunkListing expected) { + assertThat(result.chunks.length).isEqualTo(expected.chunks.length); + for (int i = 0; i < result.chunks.length; i++) { + assertWithMessage("Chunk " + i) + .that(result.chunks[i].length) + .isEqualTo(expected.chunks[i].length); + assertWithMessage("Chunk " + i) + .that(result.chunks[i].hash) + .isEqualTo(expected.chunks[i].hash); + } + } +} diff --git a/packages/SettingsLib/search/Android.bp b/packages/SettingsLib/search/Android.bp index 15c8367cf727..d398aa5c44ac 100644 --- a/packages/SettingsLib/search/Android.bp +++ b/packages/SettingsLib/search/Android.bp @@ -1,7 +1,9 @@ -java_library { +android_library { name: "SettingsLib-search", - host_supported: true, srcs: ["src/**/*.java"], + + sdk_version: "system_current", + min_sdk_version: "21", } java_plugin { @@ -9,9 +11,11 @@ java_plugin { processor_class: "com.android.settingslib.search.IndexableProcessor", static_libs: [ "javapoet-prebuilt-jar", - "SettingsLib-search", ], - srcs: ["processor-src/**/*.java"], + srcs: [ + "processor-src/**/*.java", + "src/com/android/settingslib/search/SearchIndexable.java" + ], java_resource_dirs: ["resources"], } diff --git a/packages/SettingsLib/search/AndroidManifest.xml b/packages/SettingsLib/search/AndroidManifest.xml new file mode 100644 index 000000000000..970728365f5c --- /dev/null +++ b/packages/SettingsLib/search/AndroidManifest.xml @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ Copyright (C) 2019 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 + --> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.settingslib.search"> + +</manifest>
\ No newline at end of file diff --git a/packages/SettingsLib/search/processor-src/com/android/settingslib/search/IndexableProcessor.java b/packages/SettingsLib/search/processor-src/com/android/settingslib/search/IndexableProcessor.java index 10fc685015b7..5dc9061a81a0 100644 --- a/packages/SettingsLib/search/processor-src/com/android/settingslib/search/IndexableProcessor.java +++ b/packages/SettingsLib/search/processor-src/com/android/settingslib/search/IndexableProcessor.java @@ -143,7 +143,7 @@ public class IndexableProcessor extends AbstractProcessor { final TypeSpec baseClass = TypeSpec.classBuilder(CLASS_BASE) .addModifiers(Modifier.PUBLIC) - .addSuperinterface(ClassName.get(SearchIndexableResources.class)) + .addSuperinterface(ClassName.get(PACKAGE, "SearchIndexableResources")) .addField(providers) .addMethod(baseConstructorBuilder.build()) .addMethod(addIndex) @@ -210,4 +210,4 @@ public class IndexableProcessor extends AbstractProcessor { mFiler = processingEnvironment.getFiler(); mMessager = processingEnvironment.getMessager(); } -} +}
\ No newline at end of file diff --git a/packages/SettingsLib/search/src/com/android/settingslib/search/Indexable.java b/packages/SettingsLib/search/src/com/android/settingslib/search/Indexable.java new file mode 100644 index 000000000000..e68b0d1d6798 --- /dev/null +++ b/packages/SettingsLib/search/src/com/android/settingslib/search/Indexable.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2019 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.settingslib.search; + +import android.content.Context; +import android.provider.SearchIndexableResource; + +import java.util.List; + +/** + * Interface for classes whose instances can provide data for indexing. + * + * See {@link android.provider.SearchIndexableResource} and {@link SearchIndexableRaw}. + */ +public interface Indexable { + + /** + * Interface for classes whose instances can provide data for indexing. + */ + interface SearchIndexProvider { + /** + * Return a list of references for indexing. + * + * See {@link android.provider.SearchIndexableResource} + * + * @param context the context. + * @param enabled hint telling if the data needs to be considered into the search results + * or not. + * @return a list of {@link android.provider.SearchIndexableResource} references. + * Can be null. + */ + List<SearchIndexableResource> getXmlResourcesToIndex(Context context, boolean enabled); + + /** + * Return a list of raw data for indexing. See {@link SearchIndexableRaw} + * + * @param context the context. + * @param enabled hint telling if the data needs to be considered into the search results + * or not. + * @return a list of {@link SearchIndexableRaw} references. Can be null. + */ + List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled); + + /** + * Return a list of data keys that cannot be indexed. See {@link SearchIndexableRaw} + * + * @param context the context. + * @return a list of {@link SearchIndexableRaw} references. Can be null. + */ + List<String> getNonIndexableKeys(Context context); + } +} diff --git a/packages/SettingsLib/search/src/com/android/settingslib/search/SearchIndexableRaw.java b/packages/SettingsLib/search/src/com/android/settingslib/search/SearchIndexableRaw.java new file mode 100644 index 000000000000..021ca3362aab --- /dev/null +++ b/packages/SettingsLib/search/src/com/android/settingslib/search/SearchIndexableRaw.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2019 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.settingslib.search; + +import android.content.Context; +import android.provider.SearchIndexableData; + +/** + * Indexable raw data for Search. + * + * This is the raw data used by the Indexer and should match its data model. + * + * See {@link Indexable} and {@link android.provider.SearchIndexableResource}. + */ +public class SearchIndexableRaw extends SearchIndexableData { + + /** + * Title's raw data. + */ + public String title; + + /** + * Summary's raw data when the data is "ON". + */ + public String summaryOn; + + /** + * Summary's raw data when the data is "OFF". + */ + public String summaryOff; + + /** + * Entries associated with the raw data (when the data can have several values). + */ + public String entries; + + /** + * Keywords' raw data. + */ + public String keywords; + + /** + * Fragment's or Activity's title associated with the raw data. + */ + public String screenTitle; + + public SearchIndexableRaw(Context context) { + super(context); + } +} diff --git a/packages/SettingsLib/search/src/com/android/settingslib/search/SearchIndexableResources.java b/packages/SettingsLib/search/src/com/android/settingslib/search/SearchIndexableResources.java index 300d360e0057..976647b3e88f 100644 --- a/packages/SettingsLib/search/src/com/android/settingslib/search/SearchIndexableResources.java +++ b/packages/SettingsLib/search/src/com/android/settingslib/search/SearchIndexableResources.java @@ -32,4 +32,4 @@ public interface SearchIndexableResources { * as a device binary. */ void addIndex(Class indexClass); -} +}
\ No newline at end of file diff --git a/packages/Shell/src/com/android/shell/BugreportProgressService.java b/packages/Shell/src/com/android/shell/BugreportProgressService.java index 602fe3ec12fd..2a41aa6bb8f6 100644 --- a/packages/Shell/src/com/android/shell/BugreportProgressService.java +++ b/packages/Shell/src/com/android/shell/BugreportProgressService.java @@ -37,6 +37,7 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; +import android.app.admin.DevicePolicyManager; import android.content.ClipData; import android.content.Context; import android.content.DialogInterface; @@ -107,6 +108,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.text.NumberFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -157,6 +160,8 @@ public class BugreportProgressService extends Service { private static final String TAG = "BugreportProgressService"; private static final boolean DEBUG = false; + private Intent startSelfIntent; + private static final String AUTHORITY = "com.android.shell"; // External intents sent by dumpstate. @@ -235,6 +240,24 @@ public class BugreportProgressService extends Service { private static final String NOTIFICATION_CHANNEL_ID = "bugreports"; + /** + * Always keep the newest 8 bugreport files. + */ + private static final int MIN_KEEP_COUNT = 8; + + /** + * Always keep bugreports taken in the last week. + */ + private static final long MIN_KEEP_AGE = DateUtils.WEEK_IN_MILLIS; + + private static final String BUGREPORT_MIMETYPE = "application/vnd.android.bugreport"; + + /** Always keep just the last 3 remote bugreport's files around. */ + private static final int REMOTE_BUGREPORT_FILES_AMOUNT = 3; + + /** Always keep remote bugreport files created in the last day. */ + private static final long REMOTE_MIN_KEEP_AGE = DateUtils.DAY_IN_MILLIS; + private final Object mLock = new Object(); /** Managed bugreport info (keyed by id) */ @@ -281,6 +304,7 @@ public class BugreportProgressService extends Service { mMainThreadHandler = new Handler(Looper.getMainLooper()); mServiceHandler = new ServiceHandler("BugreportProgressServiceMainThread"); mScreenshotHandler = new ScreenshotHandler("BugreportProgressServiceScreenshotThread"); + startSelfIntent = new Intent(this, this.getClass()); mScreenshotsDir = new File(getFilesDir(), SCREENSHOT_DIR); if (!mScreenshotsDir.exists()) { @@ -307,6 +331,9 @@ public class BugreportProgressService extends Service { public int onStartCommand(Intent intent, int flags, int startId) { Log.v(TAG, "onStartCommand(): " + dumpIntent(intent)); if (intent != null) { + if (!intent.hasExtra(EXTRA_ORIGINAL_INTENT) && !intent.hasExtra(EXTRA_ID)) { + return START_NOT_STICKY; + } // Handle it in a separate thread. final Message msg = mServiceHandler.obtainMessage(); msg.what = MSG_SERVICE_COMMAND; @@ -352,10 +379,11 @@ public class BugreportProgressService extends Service { private final BugreportInfo mInfo; - BugreportCallbackImpl(String name, @Nullable String title, @Nullable String description) { + BugreportCallbackImpl(String name, @Nullable String title, @Nullable String description, + @BugreportParams.BugreportMode int type) { // pid not used in this workflow, so setting default = 0 mInfo = new BugreportInfo(mContext, 0 /* pid */, name, - 100 /* max progress*/, title, description); + 100 /* max progress*/, title, description, type); } @Override @@ -380,10 +408,9 @@ public class BugreportProgressService extends Service { @Override public void onFinished() { + // TODO: Make all callback functions lock protected. trackInfoWithId(); - // Stop running on foreground, otherwise share notification cannot be dismissed. - onBugreportFinished(mInfo.id); - stopSelfWhenDone(); + sendBugreportFinishedBroadcast(); } /** @@ -400,6 +427,90 @@ public class BugreportProgressService extends Service { } return; } + + private void sendBugreportFinishedBroadcast() { + final String bugreportFileName = mInfo.name + ".zip"; + final File bugreportFile = new File(BUGREPORT_DIR, bugreportFileName); + final String bugreportFilePath = bugreportFile.getAbsolutePath(); + if (bugreportFile.length() == 0) { + Log.e(TAG, "Bugreport file empty. File path = " + bugreportFilePath); + return; + } + if (mInfo.type == BugreportParams.BUGREPORT_MODE_REMOTE) { + sendRemoteBugreportFinishedBroadcast(bugreportFilePath, bugreportFile); + } else { + cleanupOldFiles(MIN_KEEP_COUNT, MIN_KEEP_AGE); + final Intent intent = new Intent(INTENT_BUGREPORT_FINISHED); + intent.putExtra(EXTRA_BUGREPORT, bugreportFilePath); + addScreenshotToIntent(intent); + mContext.sendBroadcast(intent, android.Manifest.permission.DUMP); + onBugreportFinished(mInfo.id); + } + } + + private void sendRemoteBugreportFinishedBroadcast(String bugreportFileName, + File bugreportFile) { + cleanupOldFiles(REMOTE_BUGREPORT_FILES_AMOUNT, REMOTE_MIN_KEEP_AGE); + final Intent intent = new Intent(DevicePolicyManager.ACTION_REMOTE_BUGREPORT_DISPATCH); + final Uri bugreportUri = getUri(mContext, bugreportFile); + final String bugreportHash = generateFileHash(bugreportFileName); + if (bugreportHash == null) { + Log.e(TAG, "Error generating file hash for remote bugreport"); + return; + } + intent.setDataAndType(bugreportUri, BUGREPORT_MIMETYPE); + intent.putExtra(DevicePolicyManager.EXTRA_REMOTE_BUGREPORT_HASH, bugreportHash); + intent.putExtra(EXTRA_BUGREPORT, bugreportFileName); + mContext.sendBroadcastAsUser(intent, UserHandle.SYSTEM, + android.Manifest.permission.DUMP); + } + + private void addScreenshotToIntent(Intent intent) { + final String screenshotFileName = mInfo.name + ".png"; + final File screenshotFile = new File(BUGREPORT_DIR, screenshotFileName); + final String screenshotFilePath = screenshotFile.getAbsolutePath(); + if (screenshotFile.length() > 0) { + intent.putExtra(EXTRA_SCREENSHOT, screenshotFilePath); + } + return; + } + + private String generateFileHash(String fileName) { + String fileHash = null; + try { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + FileInputStream input = new FileInputStream(new File(fileName)); + byte[] buffer = new byte[65536]; + int size; + while ((size = input.read(buffer)) > 0) { + md.update(buffer, 0, size); + } + input.close(); + byte[] hashBytes = md.digest(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < hashBytes.length; i++) { + sb.append(String.format("%02x", hashBytes[i])); + } + fileHash = sb.toString(); + } catch (IOException | NoSuchAlgorithmException e) { + Log.e(TAG, "generating file hash for bugreport file failed " + fileName, e); + } + return fileHash; + } + } + + static void cleanupOldFiles(final int minCount, final long minAge) { + new AsyncTask<Void, Void, Void>() { + @Override + protected Void doInBackground(Void... params) { + try { + FileUtils.deleteOlderFiles(new File(BUGREPORT_DIR), minCount, minAge); + } catch (RuntimeException e) { + Log.e(TAG, "RuntimeException deleting old files", e); + } + return null; + } + }.execute(); } /** @@ -598,7 +709,7 @@ public class BugreportProgressService extends Service { + " screenshot file fd: " + screenshotFd); BugreportCallbackImpl bugreportCallback = new BugreportCallbackImpl(bugreportName, - shareTitle, shareDescription); + shareTitle, shareDescription, bugreportType); try { mBugreportManager.startBugreport(bugreportFd, screenshotFd, new BugreportParams(bugreportType), executor, bugreportCallback); @@ -711,6 +822,9 @@ public class BugreportProgressService extends Service { } else { mForegroundId = id; Log.d(TAG, "Start running as foreground service on id " + mForegroundId); + // Explicitly starting the service so that stopForeground() does not crash + // Workaround for b/140997620 + startForegroundService(startSelfIntent); startForeground(mForegroundId, notification); } } @@ -1925,10 +2039,19 @@ public class BugreportProgressService extends Service { String shareDescription; /** + * Type of the bugreport + */ + int type; + + /** * Constructor for tracked bugreports - typically called upon receiving BUGREPORT_STARTED. */ BugreportInfo(Context context, int id, int pid, String name, int max) { - this(context, pid, name, max, null, null); + // bugreports triggered by STARTED broadcast do not use callback functions, + // onFinished() callback method is the only function where type is used. + // Set type to -1 as it is unused in this workflow. + // This constructor will soon be removed. + this(context, pid, name, max, null, null, -1); this.id = id; } @@ -1936,13 +2059,14 @@ public class BugreportProgressService extends Service { * Constructor for tracked bugreports - typically called upon receiving BUGREPORT_REQUESTED. */ BugreportInfo(Context context, int pid, String name, int max, @Nullable String shareTitle, - @Nullable String shareDescription) { + @Nullable String shareDescription, int type) { this.context = context; this.pid = pid; this.name = name; this.max = this.realMax = max; this.shareTitle = shareTitle == null ? "" : shareTitle; this.shareDescription = shareDescription == null ? "" : shareDescription; + this.type = type; } /** diff --git a/packages/SystemUI/shared/Android.bp b/packages/SystemUI/shared/Android.bp index 31a538c65dea..447181813888 100644 --- a/packages/SystemUI/shared/Android.bp +++ b/packages/SystemUI/shared/Android.bp @@ -27,4 +27,6 @@ android_library { // Enforce that the library is built against java 7 so that there are // no compatibility issues with launcher java_version: "1.7", + + min_sdk_version: "26", } diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/recents/ISystemUiProxy.aidl b/packages/SystemUI/shared/src/com/android/systemui/shared/recents/ISystemUiProxy.aidl index 9228b178c76f..1cabee1ae679 100644 --- a/packages/SystemUI/shared/src/com/android/systemui/shared/recents/ISystemUiProxy.aidl +++ b/packages/SystemUI/shared/src/com/android/systemui/shared/recents/ISystemUiProxy.aidl @@ -109,4 +109,9 @@ interface ISystemUiProxy { * Ends the system screen pinning. */ void stopScreenPinning() = 17; + + /** + * Sets the shelf height and visibility. + */ + void setShelfHeight(boolean visible, int shelfHeight) = 20; } diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/system/PinnedStackListenerForwarder.java b/packages/SystemUI/shared/src/com/android/systemui/shared/system/PinnedStackListenerForwarder.java index 2797042ac160..fe5a57a277a4 100644 --- a/packages/SystemUI/shared/src/com/android/systemui/shared/system/PinnedStackListenerForwarder.java +++ b/packages/SystemUI/shared/src/com/android/systemui/shared/system/PinnedStackListenerForwarder.java @@ -69,13 +69,6 @@ public class PinnedStackListenerForwarder extends IPinnedStackListener.Stub { } @Override - public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) { - for (PinnedStackListener listener : mListeners) { - listener.onShelfVisibilityChanged(shelfVisible, shelfHeight); - } - } - - @Override public void onMinimizedStateChanged(boolean isMinimized) { for (PinnedStackListener listener : mListeners) { listener.onMinimizedStateChanged(isMinimized); @@ -143,8 +136,6 @@ public class PinnedStackListenerForwarder extends IPinnedStackListener.Stub { public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) {} - public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) {} - public void onMinimizedStateChanged(boolean isMinimized) {} public void onActionsChanged(ParceledListSlice actions) {} diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/system/WindowManagerWrapper.java b/packages/SystemUI/shared/src/com/android/systemui/shared/system/WindowManagerWrapper.java index 9f1a1fafeec6..ad182fe57f81 100644 --- a/packages/SystemUI/shared/src/com/android/systemui/shared/system/WindowManagerWrapper.java +++ b/packages/SystemUI/shared/src/com/android/systemui/shared/system/WindowManagerWrapper.java @@ -143,14 +143,6 @@ public class WindowManagerWrapper { } } - public void setShelfHeight(boolean visible, int shelfHeight) { - try { - WindowManagerGlobal.getWindowManagerService().setShelfHeight(visible, shelfHeight); - } catch (RemoteException e) { - Log.w(TAG, "Failed to set shelf height"); - } - } - public void setRecentsVisibility(boolean visible) { try { WindowManagerGlobal.getWindowManagerService().setRecentsVisibility(visible); diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardPasswordView.java b/packages/SystemUI/src/com/android/keyguard/KeyguardPasswordView.java index eaaa3ed78654..e3ac0f684e44 100644 --- a/packages/SystemUI/src/com/android/keyguard/KeyguardPasswordView.java +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardPasswordView.java @@ -86,13 +86,12 @@ public class KeyguardPasswordView extends KeyguardAbsKeyInputView mSecurityMessageDisplay.setMessage(""); } final boolean wasDisabled = mPasswordEntry.isEnabled(); - // Don't set enabled password entry & showSoftInput when PasswordEntry is invisible or in - // pausing stage. + setPasswordEntryEnabled(true); + setPasswordEntryInputEnabled(true); + // Don't call showSoftInput when PasswordEntry is invisible or in pausing stage. if (!mResumed || !mPasswordEntry.isVisibleToUser()) { return; } - setPasswordEntryEnabled(true); - setPasswordEntryInputEnabled(true); if (wasDisabled) { mImm.showSoftInput(mPasswordEntry, InputMethodManager.SHOW_IMPLICIT); } diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIAppComponentFactory.java b/packages/SystemUI/src/com/android/systemui/SystemUIAppComponentFactory.java index 2c8324cafca0..aa13fa834f56 100644 --- a/packages/SystemUI/src/com/android/systemui/SystemUIAppComponentFactory.java +++ b/packages/SystemUI/src/com/android/systemui/SystemUIAppComponentFactory.java @@ -92,6 +92,12 @@ public class SystemUIAppComponentFactory extends AppComponentFactory { public Activity instantiateActivityCompat(@NonNull ClassLoader cl, @NonNull String className, @Nullable Intent intent) throws InstantiationException, IllegalAccessException, ClassNotFoundException { + if (mComponentHelper == null) { + // This shouldn't happen, but is seen on occasion. + // Bug filed against framework to take a look: http://b/141008541 + SystemUIFactory.getInstance().getRootComponent().inject( + SystemUIAppComponentFactory.this); + } Activity activity = mComponentHelper.resolveActivity(className); if (activity != null) { return activity; diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIBinder.java b/packages/SystemUI/src/com/android/systemui/SystemUIBinder.java index 4531c892a022..0fa80aca97fb 100644 --- a/packages/SystemUI/src/com/android/systemui/SystemUIBinder.java +++ b/packages/SystemUI/src/com/android/systemui/SystemUIBinder.java @@ -17,6 +17,7 @@ package com.android.systemui; import com.android.systemui.keyguard.KeyguardViewMediator; +import com.android.systemui.power.PowerUI; import dagger.Binds; import dagger.Module; @@ -33,4 +34,10 @@ public abstract class SystemUIBinder { @IntoMap @ClassKey(KeyguardViewMediator.class) public abstract SystemUI bindKeyguardViewMediator(KeyguardViewMediator sysui); + + /** Inject into PowerUI. */ + @Binds + @IntoMap + @ClassKey(PowerUI.class) + public abstract SystemUI bindPowerUI(PowerUI sysui); } diff --git a/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt b/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt index f0e8c16e650a..5e977b4684dc 100644 --- a/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt +++ b/packages/SystemUI/src/com/android/systemui/broadcast/BroadcastDispatcher.kt @@ -75,7 +75,7 @@ open class BroadcastDispatcher @Inject constructor ( * @param filter A filter to determine what broadcasts should be dispatched to this receiver. * It will only take into account actions and categories for filtering. * @param handler A handler to dispatch [BroadcastReceiver.onReceive]. By default, it is the - * main handler. + * main handler. Pass `null` to use the default. * @param user A user handle to determine which broadcast should be dispatched to this receiver. * By default, it is the current user. */ @@ -83,10 +83,12 @@ open class BroadcastDispatcher @Inject constructor ( fun registerReceiver( receiver: BroadcastReceiver, filter: IntentFilter, - handler: Handler = mainHandler, + handler: Handler? = mainHandler, user: UserHandle = context.user ) { - this.handler.obtainMessage(MSG_ADD_RECEIVER, ReceiverData(receiver, filter, handler, user)) + this.handler + .obtainMessage(MSG_ADD_RECEIVER, + ReceiverData(receiver, filter, handler ?: mainHandler, user)) .sendToTarget() } diff --git a/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt b/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt index d44b63e813e6..54f9950239c2 100644 --- a/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt +++ b/packages/SystemUI/src/com/android/systemui/broadcast/UserBroadcastDispatcher.kt @@ -34,7 +34,7 @@ import java.util.concurrent.atomic.AtomicBoolean private const val MSG_REGISTER_RECEIVER = 0 private const val MSG_UNREGISTER_RECEIVER = 1 -private const val TAG = "UniversalReceiver" +private const val TAG = "UserBroadcastDispatcher" private const val DEBUG = false /** @@ -97,7 +97,7 @@ class UserBroadcastDispatcher( private val receiverToReceiverData = ArrayMap<BroadcastReceiver, MutableSet<ReceiverData>>() override fun onReceive(context: Context, intent: Intent) { - bgHandler.post(HandleBroadcastRunnable(actionsToReceivers, context, intent)) + bgHandler.post(HandleBroadcastRunnable(actionsToReceivers, context, intent, pendingResult)) } /** @@ -160,7 +160,8 @@ class UserBroadcastDispatcher( private class HandleBroadcastRunnable( val actionsToReceivers: Map<String, Set<ReceiverData>>, val context: Context, - val intent: Intent + val intent: Intent, + val pendingResult: PendingResult ) : Runnable { override fun run() { if (DEBUG) Log.w(TAG, "Dispatching $intent") @@ -171,6 +172,7 @@ class UserBroadcastDispatcher( ?.forEach { it.handler.post { if (DEBUG) Log.w(TAG, "Dispatching to ${it.receiver}") + it.receiver.pendingResult = pendingResult it.receiver.onReceive(context, intent) } } diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java index 72ada6e90cd0..824034507019 100644 --- a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java +++ b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleController.java @@ -126,8 +126,6 @@ public class BubbleController implements ConfigurationController.ConfigurationLi static final int DISMISS_GROUP_CANCELLED = 9; static final int DISMISS_INVALID_INTENT = 10; - public static final int MAX_BUBBLES = 5; // TODO: actually enforce this - private final Context mContext; private final NotificationEntryManager mNotificationEntryManager; private final BubbleTaskStackListener mTaskStackListener; diff --git a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java index 31cf853dce04..340dced1043f 100644 --- a/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java +++ b/packages/SystemUI/src/com/android/systemui/bubbles/BubbleStackView.java @@ -181,7 +181,9 @@ public class BubbleStackView extends FrameLayout { */ private float mVerticalPosPercentBeforeRotation = -1; + private int mMaxBubbles; private int mBubbleSize; + private int mBubbleElevation; private int mBubblePaddingTop; private int mBubbleTouchPadding; private int mExpandedViewPadding; @@ -326,7 +328,9 @@ public class BubbleStackView extends FrameLayout { mInflater = LayoutInflater.from(context); Resources res = getResources(); + mMaxBubbles = res.getInteger(R.integer.bubbles_max_rendered); mBubbleSize = res.getDimensionPixelSize(R.dimen.individual_bubble_size); + mBubbleElevation = res.getDimensionPixelSize(R.dimen.bubble_elevation); mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top); mBubbleTouchPadding = res.getDimensionPixelSize(R.dimen.bubble_touch_padding); mExpandedAnimateXDistance = @@ -1597,8 +1601,7 @@ public class BubbleStackView extends FrameLayout { for (int i = 0; i < bubbleCount; i++) { BubbleView bv = (BubbleView) mBubbleContainer.getChildAt(i); bv.updateDotVisibility(true /* animate */); - bv.setZ((BubbleController.MAX_BUBBLES - * getResources().getDimensionPixelSize(R.dimen.bubble_elevation)) - i); + bv.setZ((mMaxBubbles * mBubbleElevation) - i); // If the dot is on the left, and so is the stack, we need to change the dot position. if (bv.getDotPositionOnLeft() == mStackOnLeftOrWillBe) { bv.setDotPosition(!mStackOnLeftOrWillBe, animate); diff --git a/packages/SystemUI/src/com/android/systemui/globalactions/GlobalActionsDialog.java b/packages/SystemUI/src/com/android/systemui/globalactions/GlobalActionsDialog.java index 57a5ae63511c..22846bc02a38 100644 --- a/packages/SystemUI/src/com/android/systemui/globalactions/GlobalActionsDialog.java +++ b/packages/SystemUI/src/com/android/systemui/globalactions/GlobalActionsDialog.java @@ -90,6 +90,7 @@ import com.android.systemui.Dependency; import com.android.systemui.Interpolators; import com.android.systemui.MultiListLayout; import com.android.systemui.MultiListLayout.MultiListAdapter; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.colorextraction.SysuiColorExtractor; import com.android.systemui.plugins.ActivityStarter; import com.android.systemui.plugins.GlobalActions.GlobalActionsManager; @@ -187,7 +188,7 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener, filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); - context.registerReceiver(mBroadcastReceiver, filter); + Dependency.get(BroadcastDispatcher.class).registerReceiver(mBroadcastReceiver, filter); ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); diff --git a/packages/SystemUI/src/com/android/systemui/pip/BasePipManager.java b/packages/SystemUI/src/com/android/systemui/pip/BasePipManager.java index e0fc31bc70b8..05be4259dd3b 100644 --- a/packages/SystemUI/src/com/android/systemui/pip/BasePipManager.java +++ b/packages/SystemUI/src/com/android/systemui/pip/BasePipManager.java @@ -27,5 +27,6 @@ public interface BasePipManager { default void expandPip() {} default void hidePipMenu(Runnable onStartCallback, Runnable onEndCallback) {} void onConfigurationChanged(Configuration newConfig); + default void setShelfHeight(boolean visible, int height) {} default void dump(PrintWriter pw) {} } diff --git a/packages/SystemUI/src/com/android/systemui/pip/PipBoundsHandler.java b/packages/SystemUI/src/com/android/systemui/pip/PipBoundsHandler.java index 6795bff6409a..686e7db86c3b 100644 --- a/packages/SystemUI/src/com/android/systemui/pip/PipBoundsHandler.java +++ b/packages/SystemUI/src/com/android/systemui/pip/PipBoundsHandler.java @@ -55,6 +55,12 @@ public class PipBoundsHandler { private final Rect mTmpInsets = new Rect(); private final Point mTmpDisplaySize = new Point(); + /** + * Tracks the destination bounds, used for any following + * {@link #onMovementBoundsChanged(Rect, Rect, Rect, DisplayInfo)} calculations. + */ + private final Rect mLastDestinationBounds = new Rect(); + private IPinnedStackController mPinnedStackController; private ComponentName mLastPipComponentName; private float mReentrySnapFraction = INVALID_SNAP_FRACTION; @@ -120,19 +126,26 @@ public class PipBoundsHandler { } /** - * Responds to IPinnedStackListener on IME visibility change. + * Sets both shelf visibility and its height if applicable. + * @return {@code true} if the internal shelf state is changed, {@code false} otherwise. */ - public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) { - mIsImeShowing = imeVisible; - mImeHeight = imeHeight; + public boolean setShelfHeight(boolean shelfVisible, int shelfHeight) { + final boolean shelfShowing = shelfVisible && shelfHeight > 0; + if (shelfShowing == mIsShelfShowing && shelfHeight == mShelfHeight) { + return false; + } + + mIsShelfShowing = shelfVisible; + mShelfHeight = shelfHeight; + return true; } /** - * Responds to IPinnedStackListener on shelf visibility change. + * Responds to IPinnedStackListener on IME visibility change. */ - public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) { - mIsShelfShowing = shelfVisible; - mShelfHeight = shelfHeight; + public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) { + mIsImeShowing = imeVisible; + mImeHeight = imeHeight; } /** @@ -185,6 +198,10 @@ public class PipBoundsHandler { mLastPipComponentName = null; } + public Rect getLastDestinationBounds() { + return mLastDestinationBounds; + } + /** * Responds to IPinnedStackListener on {@link DisplayInfo} change. * It will normally follow up with a @@ -232,6 +249,7 @@ public class PipBoundsHandler { try { mPinnedStackController.startAnimation(destinationBounds, sourceRectHint, -1 /* animationDuration */); + mLastDestinationBounds.set(destinationBounds); } catch (RemoteException e) { Log.e(TAG, "Failed to start PiP animation from SysUI", e); } diff --git a/packages/SystemUI/src/com/android/systemui/pip/PipUI.java b/packages/SystemUI/src/com/android/systemui/pip/PipUI.java index 37c8163702cf..682c76c6136a 100644 --- a/packages/SystemUI/src/com/android/systemui/pip/PipUI.java +++ b/packages/SystemUI/src/com/android/systemui/pip/PipUI.java @@ -85,6 +85,14 @@ public class PipUI extends SystemUI implements CommandQueue.Callbacks { mPipManager.onConfigurationChanged(newConfig); } + public void setShelfHeight(boolean visible, int height) { + if (mPipManager == null) { + return; + } + + mPipManager.setShelfHeight(visible, height); + } + @Override public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (mPipManager == null) { diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java index 8dfae32a1939..369073c6564d 100644 --- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java +++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java @@ -143,14 +143,6 @@ public class PipManager implements BasePipManager { } @Override - public void onShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) { - mHandler.post(() -> { - mPipBoundsHandler.onShelfVisibilityChanged(shelfVisible, shelfHeight); - mTouchHandler.onShelfVisibilityChanged(shelfVisible, shelfHeight); - }); - } - - @Override public void onMinimizedStateChanged(boolean isMinimized) { mHandler.post(() -> { mPipBoundsHandler.onMinimizedStateChanged(isMinimized); @@ -161,14 +153,8 @@ public class PipManager implements BasePipManager { @Override public void onMovementBoundsChanged(Rect animatingBounds, boolean fromImeAdjustment, boolean fromShelfAdjustment) { - mHandler.post(() -> { - // Populate the inset / normal bounds and DisplayInfo from mPipBoundsHandler first. - mPipBoundsHandler.onMovementBoundsChanged(mTmpInsetBounds, mTmpNormalBounds, - animatingBounds, mTmpDisplayInfo); - mTouchHandler.onMovementBoundsChanged(mTmpInsetBounds, mTmpNormalBounds, - animatingBounds, fromImeAdjustment, fromShelfAdjustment, - mTmpDisplayInfo.rotation); - }); + mHandler.post(() -> updateMovementBounds(animatingBounds, fromImeAdjustment, + fromShelfAdjustment)); } @Override @@ -280,6 +266,31 @@ public class PipManager implements BasePipManager { } /** + * Sets both shelf visibility and its height. + */ + @Override + public void setShelfHeight(boolean visible, int height) { + mHandler.post(() -> { + final boolean changed = mPipBoundsHandler.setShelfHeight(visible, height); + if (changed) { + mTouchHandler.onShelfVisibilityChanged(visible, height); + updateMovementBounds(mPipBoundsHandler.getLastDestinationBounds(), + false /* fromImeAdjustment */, true /* fromShelfAdjustment */); + } + }); + } + + private void updateMovementBounds(Rect animatingBounds, boolean fromImeAdjustment, + boolean fromShelfAdjustment) { + // Populate inset / normal bounds and DisplayInfo from mPipBoundsHandler first. + mPipBoundsHandler.onMovementBoundsChanged(mTmpInsetBounds, mTmpNormalBounds, + animatingBounds, mTmpDisplayInfo); + mTouchHandler.onMovementBoundsChanged(mTmpInsetBounds, mTmpNormalBounds, + animatingBounds, fromImeAdjustment, fromShelfAdjustment, + mTmpDisplayInfo.rotation); + } + + /** * Gets an instance of {@link PipManager}. */ public static PipManager getInstance() { diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java index 4f2a6d82a08e..5723afd4ae95 100644 --- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java +++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java @@ -247,6 +247,9 @@ public class PipMenuActivity extends Activity { protected void onStop() { super.onStop(); + // In cases such as device lock, hide and finish it so that it can be recreated on the top + // next time it starts, see also {@link #onUserLeaveHint} + hideMenu(); cancelDelayedFinish(); } diff --git a/packages/SystemUI/src/com/android/systemui/power/PowerUI.java b/packages/SystemUI/src/com/android/systemui/power/PowerUI.java index 75dc39722bcf..a258f356bf53 100644 --- a/packages/SystemUI/src/com/android/systemui/power/PowerUI.java +++ b/packages/SystemUI/src/com/android/systemui/power/PowerUI.java @@ -45,6 +45,7 @@ import com.android.settingslib.utils.ThreadUtils; import com.android.systemui.Dependency; import com.android.systemui.R; import com.android.systemui.SystemUI; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.statusbar.phone.StatusBar; import java.io.FileDescriptor; @@ -53,6 +54,8 @@ import java.time.Duration; import java.util.Arrays; import java.util.concurrent.Future; +import javax.inject.Inject; + public class PowerUI extends SystemUI { static final String TAG = "PowerUI"; @@ -97,6 +100,12 @@ public class PowerUI extends SystemUI { private IThermalEventListener mSkinThermalEventListener; private IThermalEventListener mUsbThermalEventListener; + private final BroadcastDispatcher mBroadcastDispatcher; + + @Inject + public PowerUI(BroadcastDispatcher broadcastDispatcher) { + mBroadcastDispatcher = broadcastDispatcher; + } public void start() { mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); @@ -211,7 +220,7 @@ public class PowerUI extends SystemUI { filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_USER_SWITCHED); - mContext.registerReceiver(this, filter, null, mHandler); + mBroadcastDispatcher.registerReceiver(this, filter, mHandler); } @Override diff --git a/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java b/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java index d20b22815805..4013586d4197 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java +++ b/packages/SystemUI/src/com/android/systemui/qs/QuickStatusBarHeader.java @@ -257,10 +257,6 @@ public class QuickStatusBarHeader extends RelativeLayout implements mNextAlarmTextView.setSelected(true); mPermissionsHubEnabled = PrivacyItemControllerKt.isPermissionsHubEnabled(); - // Change the ignored slots when DeviceConfig flag changes - DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_PRIVACY, - mContext.getMainExecutor(), mPropertiesListener); - } private List<String> getIgnoredIconSlots() { @@ -489,6 +485,9 @@ public class QuickStatusBarHeader extends RelativeLayout implements super.onAttachedToWindow(); mStatusBarIconController.addIconGroup(mIconManager); requestApplyInsets(); + // Change the ignored slots when DeviceConfig flag changes + DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_PRIVACY, + mContext.getMainExecutor(), mPropertiesListener); } @Override @@ -527,6 +526,7 @@ public class QuickStatusBarHeader extends RelativeLayout implements public void onDetachedFromWindow() { setListening(false); mStatusBarIconController.removeIconGroup(mIconManager); + DeviceConfig.removeOnPropertiesChangedListener(mPropertiesListener); super.onDetachedFromWindow(); } diff --git a/packages/SystemUI/src/com/android/systemui/recents/OverviewProxyService.java b/packages/SystemUI/src/com/android/systemui/recents/OverviewProxyService.java index 9268ee0705a2..e0ae8ed66f41 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/OverviewProxyService.java +++ b/packages/SystemUI/src/com/android/systemui/recents/OverviewProxyService.java @@ -58,6 +58,7 @@ import com.android.internal.policy.ScreenDecorationsUtils; import com.android.systemui.Dumpable; import com.android.systemui.SysUiServiceProvider; import com.android.systemui.model.SysUiState; +import com.android.systemui.pip.PipUI; import com.android.systemui.recents.OverviewProxyService.OverviewProxyListener; import com.android.systemui.shared.recents.IOverviewProxy; import com.android.systemui.shared.recents.ISystemUiProxy; @@ -353,6 +354,20 @@ public class OverviewProxyService implements CallbackController<OverviewProxyLis } } + @Override + public void setShelfHeight(boolean visible, int shelfHeight) { + if (!verifyCaller("setShelfHeight")) { + return; + } + long token = Binder.clearCallingIdentity(); + try { + final PipUI component = SysUiServiceProvider.getComponent(mContext, PipUI.class); + component.setShelfHeight(visible, shelfHeight); + } finally { + Binder.restoreCallingIdentity(token); + } + } + private boolean verifyCaller(String reason) { final int callerId = Binder.getCallingUserHandle().getIdentifier(); if (callerId != mCurrentBoundedUserId) { diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsOnboarding.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsOnboarding.java index d0c47345a83a..c1ce16337f8d 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/RecentsOnboarding.java +++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsOnboarding.java @@ -61,8 +61,10 @@ import android.view.animation.DecelerateInterpolator; import android.widget.ImageView; import android.widget.TextView; +import com.android.systemui.Dependency; import com.android.systemui.Prefs; import com.android.systemui.R; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.shared.recents.IOverviewProxy; import com.android.systemui.shared.system.ActivityManagerWrapper; import com.android.systemui.shared.system.QuickStepContract; @@ -245,10 +247,15 @@ public class RecentsOnboarding { private final View.OnAttachStateChangeListener mOnAttachStateChangeListener = new View.OnAttachStateChangeListener() { + + private final BroadcastDispatcher mBroadcastDispatcher = Dependency.get( + BroadcastDispatcher.class); + @Override public void onViewAttachedToWindow(View view) { if (view == mLayout) { - mContext.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF)); + mBroadcastDispatcher.registerReceiver(mReceiver, + new IntentFilter(Intent.ACTION_SCREEN_OFF)); mLayoutAttachedToWindow = true; if (view.getTag().equals(R.string.recents_swipe_up_onboarding)) { mHasDismissedSwipeUpTip = false; @@ -273,7 +280,7 @@ public class RecentsOnboarding { } mOverviewOpenedCountSinceQuickScrubTipDismiss = 0; } - mContext.unregisterReceiver(mReceiver); + mBroadcastDispatcher.unregisterReceiver(mReceiver); } } }; @@ -335,10 +342,11 @@ public class RecentsOnboarding { private void notifyOnTip(int action, int target) { try { IOverviewProxy overviewProxy = mOverviewProxyService.getProxy(); - if(overviewProxy != null) { + if (overviewProxy != null) { overviewProxy.onTip(action, target); } - } catch (RemoteException e) {} + } catch (RemoteException e) { + } } public void onNavigationModeChanged(int mode) { diff --git a/packages/SystemUI/src/com/android/systemui/recents/ScreenPinningRequest.java b/packages/SystemUI/src/com/android/systemui/recents/ScreenPinningRequest.java index c3c0d63f66c4..0f277ca8b2c6 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/ScreenPinningRequest.java +++ b/packages/SystemUI/src/com/android/systemui/recents/ScreenPinningRequest.java @@ -47,6 +47,7 @@ import android.widget.TextView; import com.android.systemui.Dependency; import com.android.systemui.R; import com.android.systemui.SysUiServiceProvider; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.shared.system.QuickStepContract; import com.android.systemui.shared.system.WindowManagerWrapper; import com.android.systemui.statusbar.phone.NavigationBarView; @@ -159,6 +160,8 @@ public class ScreenPinningRequest implements View.OnClickListener, private ValueAnimator mColorAnim; private ViewGroup mLayout; private boolean mShowCancel; + private final BroadcastDispatcher mBroadcastDispatcher = + Dependency.get(BroadcastDispatcher.class); public RequestWindowView(Context context, boolean showCancel) { super(context); @@ -212,7 +215,7 @@ public class ScreenPinningRequest implements View.OnClickListener, IntentFilter filter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED); filter.addAction(Intent.ACTION_USER_SWITCHED); filter.addAction(Intent.ACTION_SCREEN_OFF); - mContext.registerReceiver(mReceiver, filter); + mBroadcastDispatcher.registerReceiver(mReceiver, filter); } private void inflateView(int rotation) { @@ -313,7 +316,7 @@ public class ScreenPinningRequest implements View.OnClickListener, @Override public void onDetachedFromWindow() { - mContext.unregisterReceiver(mReceiver); + mBroadcastDispatcher.unregisterReceiver(mReceiver); } protected void onConfigurationChanged() { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java index 14009214bdc5..6c0f90a65ae9 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java @@ -21,6 +21,7 @@ import static com.android.systemui.statusbar.notification.NotificationEntryManag import static com.android.systemui.statusbar.phone.StatusBar.DEBUG; import static com.android.systemui.statusbar.phone.StatusBar.ENABLE_CHILD_NOTIFICATIONS; +import android.annotation.Nullable; import android.annotation.SuppressLint; import android.app.NotificationManager; import android.content.ComponentName; @@ -57,8 +58,9 @@ public class NotificationListener extends NotificationListenerWithPlugins { private final NotificationGroupManager mGroupManager = Dependency.get(NotificationGroupManager.class); - private final ArrayList<NotificationSettingsListener> mSettingsListeners = new ArrayList<>(); private final Context mContext; + private final ArrayList<NotificationSettingsListener> mSettingsListeners = new ArrayList<>(); + @Nullable private NotifServiceListener mDownstreamListener; @Inject public NotificationListener(Context context) { @@ -69,6 +71,10 @@ public class NotificationListener extends NotificationListenerWithPlugins { mSettingsListeners.add(listener); } + public void setDownstreamListener(NotifServiceListener downstreamListener) { + mDownstreamListener = downstreamListener; + } + @Override public void onListenerConnected() { if (DEBUG) Log.d(TAG, "onListenerConnected"); @@ -81,6 +87,9 @@ public class NotificationListener extends NotificationListenerWithPlugins { final RankingMap currentRanking = getCurrentRanking(); Dependency.get(Dependency.MAIN_HANDLER).post(() -> { for (StatusBarNotification sbn : notifications) { + if (mDownstreamListener != null) { + mDownstreamListener.onNotificationPosted(sbn, currentRanking); + } mEntryManager.addNotification(sbn, currentRanking); } }); @@ -95,6 +104,11 @@ public class NotificationListener extends NotificationListenerWithPlugins { if (sbn != null && !onPluginNotificationPosted(sbn, rankingMap)) { Dependency.get(Dependency.MAIN_HANDLER).post(() -> { processForRemoteInput(sbn.getNotification(), mContext); + + if (mDownstreamListener != null) { + mDownstreamListener.onNotificationPosted(sbn, rankingMap); + } + String key = sbn.getKey(); boolean isUpdate = mEntryManager.getNotificationData().get(key) != null; @@ -133,6 +147,9 @@ public class NotificationListener extends NotificationListenerWithPlugins { if (sbn != null && !onPluginNotificationRemoved(sbn, rankingMap)) { final String key = sbn.getKey(); Dependency.get(Dependency.MAIN_HANDLER).post(() -> { + if (mDownstreamListener != null) { + mDownstreamListener.onNotificationRemoved(sbn, rankingMap, reason); + } mEntryManager.removeNotification(key, rankingMap, reason); }); } @@ -149,6 +166,9 @@ public class NotificationListener extends NotificationListenerWithPlugins { if (rankingMap != null) { RankingMap r = onPluginRankingUpdate(rankingMap); Dependency.get(Dependency.MAIN_HANDLER).post(() -> { + if (mDownstreamListener != null) { + mDownstreamListener.onNotificationRankingUpdate(rankingMap); + } mEntryManager.updateNotificationRanking(r); }); } @@ -175,4 +195,12 @@ public class NotificationListener extends NotificationListenerWithPlugins { default void onStatusBarIconsBehaviorChanged(boolean hideSilentStatusIcons) { } } + + /** Interface for listening to add/remove events that we receive from NotificationManager. */ + public interface NotifServiceListener { + void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap); + void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap); + void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason); + void onNotificationRankingUpdate(RankingMap rankingMap); + } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotifPipelineInitializer.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotifPipelineInitializer.java new file mode 100644 index 000000000000..df70828a46be --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotifPipelineInitializer.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2019 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.systemui.statusbar.notification; + +import android.service.notification.NotificationListenerService; +import android.service.notification.StatusBarNotification; +import android.util.Log; + +import com.android.systemui.statusbar.NotificationListener; + +import javax.inject.Inject; + +/** + * Initialization code for the new notification pipeline. + */ +public class NotifPipelineInitializer { + + @Inject + public NotifPipelineInitializer() { + } + + public void initialize( + NotificationListener notificationService) { + + // TODO Put real code here + notificationService.setDownstreamListener(new NotificationListener.NotifServiceListener() { + @Override + public void onNotificationPosted(StatusBarNotification sbn, + NotificationListenerService.RankingMap rankingMap) { + Log.d(TAG, "onNotificationPosted " + sbn.getKey()); + } + + @Override + public void onNotificationRemoved(StatusBarNotification sbn, + NotificationListenerService.RankingMap rankingMap) { + Log.d(TAG, "onNotificationRemoved " + sbn.getKey()); + } + + @Override + public void onNotificationRemoved(StatusBarNotification sbn, + NotificationListenerService.RankingMap rankingMap, int reason) { + Log.d(TAG, "onNotificationRemoved " + sbn.getKey()); + } + + @Override + public void onNotificationRankingUpdate( + NotificationListenerService.RankingMap rankingMap) { + Log.d(TAG, "onNotificationRankingUpdate"); + } + }); + } + + private static final String TAG = "NotifInitializer"; +} diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationEntryManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationEntryManager.java index b6b149dd049a..90301c59dc68 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationEntryManager.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationEntryManager.java @@ -22,6 +22,7 @@ import android.annotation.Nullable; import android.app.Notification; import android.content.Context; import android.service.notification.NotificationListenerService; +import android.service.notification.NotificationListenerService.Ranking; import android.service.notification.StatusBarNotification; import android.util.ArrayMap; import android.util.Log; @@ -390,7 +391,7 @@ public class NotificationEntryManager implements } mNotificationData.updateRanking(rankingMap); - NotificationListenerService.Ranking ranking = new NotificationListenerService.Ranking(); + Ranking ranking = new Ranking(); rankingMap.getRanking(key, ranking); NotificationEntry entry = new NotificationEntry(notification, ranking); @@ -513,10 +514,11 @@ public class NotificationEntryManager implements if (rankingMap == null) { return; } - NotificationListenerService.Ranking ranking = new NotificationListenerService.Ranking(); for (NotificationEntry pendingNotification : mPendingNotifications.values()) { - rankingMap.getRanking(pendingNotification.key, ranking); - pendingNotification.setRanking(ranking); + Ranking ranking = new Ranking(); + if (rankingMap.getRanking(pendingNotification.key(), ranking)) { + pendingNotification.setRanking(ranking); + } } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java index b87140dddec3..6e61d7ceaf6c 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java @@ -89,6 +89,7 @@ import com.android.systemui.R; import com.android.systemui.SysUiServiceProvider; import com.android.systemui.assist.AssistHandleViewController; import com.android.systemui.assist.AssistManager; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.fragments.FragmentHostManager; import com.android.systemui.fragments.FragmentHostManager.FragmentListener; import com.android.systemui.model.SysUiState; @@ -139,6 +140,7 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback private final MetricsLogger mMetricsLogger; private final DeviceProvisionedController mDeviceProvisionedController; private final StatusBarStateController mStatusBarStateController; + private final NavigationModeController mNavigationModeController; protected NavigationBarView mNavigationBarView = null; @@ -170,6 +172,8 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback private OverviewProxyService mOverviewProxyService; + private final BroadcastDispatcher mBroadcastDispatcher; + @VisibleForTesting public int mDisplayId; private boolean mIsOnDefaultDisplay; @@ -251,7 +255,8 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback AssistManager assistManager, OverviewProxyService overviewProxyService, NavigationModeController navigationModeController, StatusBarStateController statusBarStateController, - SysUiState sysUiFlagsContainer) { + SysUiState sysUiFlagsContainer, + BroadcastDispatcher broadcastDispatcher) { mAccessibilityManagerWrapper = accessibilityManagerWrapper; mDeviceProvisionedController = deviceProvisionedController; mStatusBarStateController = statusBarStateController; @@ -260,7 +265,9 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback mSysUiFlagsContainer = sysUiFlagsContainer; mAssistantAvailable = mAssistManager.getAssistInfoForUser(UserHandle.USER_CURRENT) != null; mOverviewProxyService = overviewProxyService; + mNavigationModeController = navigationModeController; mNavBarMode = navigationModeController.addListener(this); + mBroadcastDispatcher = broadcastDispatcher; } // ----- Fragment Lifecycle Callbacks ----- @@ -299,6 +306,7 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback @Override public void onDestroy() { super.onDestroy(); + mNavigationModeController.removeListener(this); mAccessibilityManagerWrapper.removeCallback(mAccessibilityListener); mContentResolver.unregisterContentObserver(mMagnificationObserver); mContentResolver.unregisterContentObserver(mAssistContentObserver); @@ -337,7 +345,8 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_USER_SWITCHED); - getContext().registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null); + mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, Handler.getMain(), + UserHandle.ALL); notifyNavigationBarScreenOn(); mOverviewProxyService.addCallback(mOverviewProxyListener); @@ -380,7 +389,7 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback mNavigationBarView.getLightTransitionsController().destroy(getContext()); } mOverviewProxyService.removeCallback(mOverviewProxyListener); - getContext().unregisterReceiver(mBroadcastReceiver); + mBroadcastDispatcher.unregisterReceiver(mBroadcastReceiver); } @Override diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java index fa4812dc4876..a1a47e1305f5 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java @@ -829,7 +829,11 @@ public class NavigationBarView extends FrameLayout implements mRecentsOnboarding.onNavigationModeChanged(mNavBarMode); getRotateSuggestionButton().onNavigationModeChanged(mNavBarMode); - mRegionSamplingHelper.start(mSamplingBounds); + if (isGesturalMode(mNavBarMode)) { + mRegionSamplingHelper.start(mSamplingBounds); + } else { + mRegionSamplingHelper.stop(); + } } public void setAccessibilityButtonState(final boolean visible, final boolean longClickable) { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/RegionSamplingHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/RegionSamplingHelper.java index c1ff572bb210..1a6b415f87db 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/RegionSamplingHelper.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/RegionSamplingHelper.java @@ -127,6 +127,11 @@ public class RegionSamplingHelper implements View.OnAttachStateChangeListener, updateSamplingListener(); } + void stopAndDestroy() { + stop(); + mSamplingListener.destroy(); + } + @Override public void onViewAttachedToWindow(View view) { updateSamplingListener(); @@ -134,9 +139,7 @@ public class RegionSamplingHelper implements View.OnAttachStateChangeListener, @Override public void onViewDetachedFromWindow(View view) { - // isAttachedToWindow is only changed after this call to the listeners, so let's post it - // instead - postUpdateSamplingListener(); + stopAndDestroy(); } @Override diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java index f06fbbd80dfc..7bab7f159b7e 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBar.java @@ -147,6 +147,7 @@ import com.android.systemui.SystemUIFactory; import com.android.systemui.UiOffloadThread; import com.android.systemui.appops.AppOpsController; import com.android.systemui.assist.AssistManager; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.bubbles.BubbleController; import com.android.systemui.charging.WirelessChargingAnimation; import com.android.systemui.classifier.FalsingLog; @@ -197,6 +198,7 @@ import com.android.systemui.statusbar.VibratorHelper; import com.android.systemui.statusbar.notification.ActivityLaunchAnimator; import com.android.systemui.statusbar.notification.BypassHeadsUpNotifier; import com.android.systemui.statusbar.notification.DynamicPrivacyController; +import com.android.systemui.statusbar.notification.NotifPipelineInitializer; import com.android.systemui.statusbar.notification.NotificationActivityStarter; import com.android.systemui.statusbar.notification.NotificationAlertingManager; import com.android.systemui.statusbar.notification.NotificationClicker; @@ -222,7 +224,6 @@ import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceP import com.android.systemui.statusbar.policy.ExtensionController; import com.android.systemui.statusbar.policy.HeadsUpManager; import com.android.systemui.statusbar.policy.KeyguardStateController; -import com.android.systemui.statusbar.policy.KeyguardStateControllerImpl; import com.android.systemui.statusbar.policy.KeyguardUserSwitcher; import com.android.systemui.statusbar.policy.NetworkController; import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; @@ -390,6 +391,11 @@ public class StatusBar extends SystemUI implements DemoMode, @Inject @Named(ALLOW_NOTIFICATION_LONG_PRESS_NAME) boolean mAllowNotificationLongPress; + @Inject + protected NotifPipelineInitializer mNotifPipelineInitializer; + + @VisibleForTesting + BroadcastDispatcher mBroadcastDispatcher; // expanded notifications protected NotificationPanelView mNotificationPanel; // the sliding/resizing panel within the notification window @@ -665,6 +671,7 @@ public class StatusBar extends SystemUI implements DemoMode, mBubbleController = Dependency.get(BubbleController.class); mBubbleController.setExpandListener(mBubbleExpandListener); mActivityIntentHelper = new ActivityIntentHelper(mContext); + mBroadcastDispatcher = Dependency.get(BroadcastDispatcher.class); KeyguardSliceProvider sliceProvider = KeyguardSliceProvider.getAttachedInstance(); if (sliceProvider != null) { sliceProvider.initDependencies(mMediaManager, mStatusBarStateController, @@ -1046,11 +1053,7 @@ public class StatusBar extends SystemUI implements DemoMode, } // receive broadcasts - IntentFilter filter = new IntentFilter(); - filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); - filter.addAction(Intent.ACTION_SCREEN_OFF); - filter.addAction(DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG); - context.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, filter, null, null); + registerBroadcastReceiver(); IntentFilter demoFilter = new IntentFilter(); if (DEBUG_MEDIA_FAKE_ARTWORK) { @@ -1071,6 +1074,15 @@ public class StatusBar extends SystemUI implements DemoMode, ThreadedRenderer.overrideProperty("ambientRatio", String.valueOf(1.5f)); } + @VisibleForTesting + protected void registerBroadcastReceiver() { + IntentFilter filter = new IntentFilter(); + filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); + filter.addAction(Intent.ACTION_SCREEN_OFF); + filter.addAction(DevicePolicyManager.ACTION_SHOW_DEVICE_MONITORING_DIALOG); + mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter, null, UserHandle.ALL); + } + protected QS createDefaultQSFragment() { return FragmentHostManager.get(mStatusBarWindow).create(QSFragment.class); } @@ -1128,6 +1140,8 @@ public class StatusBar extends SystemUI implements DemoMode, mGroupAlertTransferHelper.bind(mEntryManager, mGroupManager); mNotificationListController.bind(); + + mNotifPipelineInitializer.initialize(mNotificationListener); } /** diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java index 5bda34d64b73..ce929b7c621b 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/SystemUIDialog.java @@ -28,6 +28,7 @@ import android.view.WindowManager.LayoutParams; import com.android.systemui.Dependency; import com.android.systemui.R; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.statusbar.policy.KeyguardStateController; @@ -138,20 +139,21 @@ public class SystemUIDialog extends AlertDialog { private final Dialog mDialog; private boolean mRegistered; + private final BroadcastDispatcher mBroadcastDispatcher; DismissReceiver(Dialog dialog) { mDialog = dialog; + mBroadcastDispatcher = Dependency.get(BroadcastDispatcher.class); } void register() { - mDialog.getContext() - .registerReceiverAsUser(this, UserHandle.CURRENT, INTENT_FILTER, null, null); + mBroadcastDispatcher.registerReceiver(this, INTENT_FILTER, null, UserHandle.CURRENT); mRegistered = true; } void unregister() { if (mRegistered) { - mDialog.getContext().unregisterReceiver(this); + mBroadcastDispatcher.unregisterReceiver(this); mRegistered = false; } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java index c2c3f81527e8..b331fc3bf0ce 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/Clock.java @@ -16,6 +16,8 @@ package com.android.systemui.statusbar.policy; +import static com.android.systemui.util.InjectionInflationController.VIEW_CONTEXT; + import android.app.StatusBarManager; import android.content.BroadcastReceiver; import android.content.Context; @@ -44,6 +46,7 @@ import com.android.systemui.Dependency; import com.android.systemui.FontSizeUtils; import com.android.systemui.R; import com.android.systemui.SysUiServiceProvider; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.plugins.DarkIconDispatcher; import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver; import com.android.systemui.settings.CurrentUserTracker; @@ -60,6 +63,9 @@ import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; +import javax.inject.Inject; +import javax.inject.Named; + /** * Digital clock for the status bar. */ @@ -107,15 +113,20 @@ public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.C */ private int mNonAdaptedColor; - public Clock(Context context) { - this(context, null); - } + private final BroadcastDispatcher mBroadcastDispatcher; public Clock(Context context, AttributeSet attrs) { - this(context, attrs, 0); + this(context, attrs, null); + } + + @Inject + public Clock(@Named(VIEW_CONTEXT) Context context, AttributeSet attrs, + BroadcastDispatcher broadcastDispatcher) { + this(context, attrs, 0, broadcastDispatcher); } - public Clock(Context context, AttributeSet attrs, int defStyle) { + public Clock(Context context, AttributeSet attrs, int defStyle, + BroadcastDispatcher broadcastDispatcher) { super(context, attrs, defStyle); TypedArray a = context.getTheme().obtainStyledAttributes( attrs, @@ -134,6 +145,7 @@ public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.C mCurrentUserId = newUserId; } }; + mBroadcastDispatcher = broadcastDispatcher; } @Override @@ -358,11 +370,11 @@ public class Clock extends TextView implements DemoMode, Tunable, CommandQueue.C } IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_SCREEN_ON); - mContext.registerReceiver(mScreenReceiver, filter); + mBroadcastDispatcher.registerReceiver(mScreenReceiver, filter); } } else { if (mSecondsHandler != null) { - mContext.unregisterReceiver(mScreenReceiver); + mBroadcastDispatcher.unregisterReceiver(mScreenReceiver); mSecondsHandler.removeCallbacks(mSecondTick); mSecondsHandler = null; updateClock(); diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardStateControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardStateControllerImpl.java index f8c7532ec281..cc91bc082871 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardStateControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardStateControllerImpl.java @@ -101,7 +101,9 @@ public class KeyguardStateControllerImpl extends KeyguardUpdateMonitorCallback @Override public void addCallback(@NonNull Callback callback) { Preconditions.checkNotNull(callback, "Callback must not be null. b/128895449"); - mCallbacks.add(callback); + if (!mCallbacks.contains(callback)) { + mCallbacks.add(callback); + } if (mCallbacks.size() != 0 && !mListening) { mListening = true; mKeyguardUpdateMonitor.registerCallback(this); diff --git a/packages/SystemUI/src/com/android/systemui/util/InjectionInflationController.java b/packages/SystemUI/src/com/android/systemui/util/InjectionInflationController.java index e44e58a84dc8..7e801da9cd1b 100644 --- a/packages/SystemUI/src/com/android/systemui/util/InjectionInflationController.java +++ b/packages/SystemUI/src/com/android/systemui/util/InjectionInflationController.java @@ -37,6 +37,7 @@ import com.android.systemui.statusbar.NotificationShelf; import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout; import com.android.systemui.statusbar.phone.LockIcon; import com.android.systemui.statusbar.phone.NotificationPanelView; +import com.android.systemui.statusbar.policy.Clock; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -178,6 +179,11 @@ public class InjectionInflationController { * Creates the QSCustomizer. */ QSCustomizer createQSCustomizer(); + + /** + * Creates a Clock. + */ + Clock createClock(); } /** diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java index a6b5b38fd728..edea92f5952a 100644 --- a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogControllerImpl.java @@ -59,6 +59,7 @@ import com.android.settingslib.volume.MediaSessions; import com.android.systemui.Dumpable; import com.android.systemui.R; import com.android.systemui.SysUiServiceProvider; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.keyguard.WakefulnessLifecycle; import com.android.systemui.plugins.VolumeDialogController; import com.android.systemui.qs.tiles.DndTile; @@ -137,9 +138,10 @@ public class VolumeDialogControllerImpl implements VolumeDialogController, Dumpa private UserActivityListener mUserActivityListener; protected final VC mVolumeController = new VC(); + protected final BroadcastDispatcher mBroadcastDispatcher; @Inject - public VolumeDialogControllerImpl(Context context) { + public VolumeDialogControllerImpl(Context context, BroadcastDispatcher broadcastDispatcher) { mContext = context.getApplicationContext(); mNotificationManager = (NotificationManager) mContext.getSystemService( Context.NOTIFICATION_SERVICE); @@ -152,6 +154,7 @@ public class VolumeDialogControllerImpl implements VolumeDialogController, Dumpa mAudio = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); mNoMan = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); mObserver = new SettingObserver(mWorker); + mBroadcastDispatcher = broadcastDispatcher; mObserver.init(); mReceiver.init(); mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); @@ -1004,11 +1007,11 @@ public class VolumeDialogControllerImpl implements VolumeDialogController, Dumpa filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); - mContext.registerReceiver(this, filter, null, mWorker); + mBroadcastDispatcher.registerReceiver(this, filter, mWorker); } public void destroy() { - mContext.unregisterReceiver(this); + mBroadcastDispatcher.unregisterReceiver(this); } @Override diff --git a/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt b/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt index 011c2cd57588..e838d9e94a31 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/broadcast/UserBroadcastDispatcherTest.kt @@ -70,6 +70,8 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { private lateinit var mockContext: Context @Mock private lateinit var mockHandler: Handler + @Mock + private lateinit var mPendingResult: BroadcastReceiver.PendingResult @Captor private lateinit var argumentCaptor: ArgumentCaptor<IntentFilter> @@ -88,6 +90,7 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { universalBroadcastReceiver = UserBroadcastDispatcher( mockContext, USER_ID, handler, testableLooper.looper) + universalBroadcastReceiver.pendingResult = mPendingResult } @Test @@ -227,4 +230,19 @@ class UserBroadcastDispatcherTest : SysuiTestCase() { verify(broadcastReceiver).onReceive(mockContext, intent) verify(broadcastReceiverOther).onReceive(mockContext, intent) } + + @Test + fun testPendingResult() { + intentFilter = IntentFilter(ACTION_1) + universalBroadcastReceiver.registerReceiver( + ReceiverData(broadcastReceiver, intentFilter, handler, USER_HANDLE)) + + val intent = Intent(ACTION_1) + universalBroadcastReceiver.onReceive(mockContext, intent) + + testableLooper.processAllMessages() + + verify(broadcastReceiver).onReceive(mockContext, intent) + verify(broadcastReceiver).pendingResult = mPendingResult + } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java b/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java index 4d95f3f474b5..4958c649d532 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java @@ -19,6 +19,7 @@ import static android.provider.Settings.Global.SHOW_USB_TEMPERATURE_ALARM; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.anyObject; import static org.mockito.Mockito.mock; @@ -27,8 +28,11 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.IntentFilter; import android.os.BatteryManager; +import android.os.Handler; import android.os.IThermalEventListener; import android.os.IThermalService; import android.os.PowerManager; @@ -43,6 +47,7 @@ import android.testing.TestableResources; import com.android.settingslib.fuelgauge.Estimate; import com.android.systemui.R; import com.android.systemui.SysuiTestCase; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.power.PowerUI.WarningsUI; import com.android.systemui.statusbar.phone.StatusBar; @@ -80,6 +85,7 @@ public class PowerUITest extends SysuiTestCase { @Mock private IThermalService mThermalServiceMock; private IThermalEventListener mUsbThermalEventListener; private IThermalEventListener mSkinThermalEventListener; + @Mock private BroadcastDispatcher mBroadcastDispatcher; @Before public void setup() { @@ -96,6 +102,15 @@ public class PowerUITest extends SysuiTestCase { } @Test + public void testReceiverIsRegisteredToDispatcherOnStart() { + mPowerUI.start(); + verify(mBroadcastDispatcher).registerReceiver( + any(BroadcastReceiver.class), + any(IntentFilter.class), + any(Handler.class)); //PowerUI does not call with User + } + + @Test public void testSkinWarning_throttlingCritical() throws Exception { mPowerUI.start(); @@ -667,7 +682,7 @@ public class PowerUITest extends SysuiTestCase { } private void createPowerUi() { - mPowerUI = new PowerUI(); + mPowerUI = new PowerUI(mBroadcastDispatcher); mPowerUI.mContext = mContext; mPowerUI.mComponents = mContext.getComponents(); mPowerUI.mThermalService = mThermalServiceMock; diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarFragmentTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarFragmentTest.java index 33d3ac848f0f..0bff5aa9e991 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarFragmentTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/NavigationBarFragmentTest.java @@ -24,9 +24,11 @@ import static android.view.DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.annotation.LayoutRes; @@ -34,11 +36,14 @@ import android.annotation.Nullable; import android.app.Fragment; import android.app.FragmentController; import android.app.FragmentHostCallback; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.IntentFilter; import android.hardware.display.DisplayManagerGlobal; import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import android.os.UserHandle; import android.testing.AndroidTestingRunner; import android.testing.LeakCheck.Tracker; import android.testing.TestableLooper; @@ -58,6 +63,7 @@ import com.android.systemui.Dependency; import com.android.systemui.SysuiBaseFragmentTest; import com.android.systemui.SysuiTestableContext; import com.android.systemui.assist.AssistManager; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.model.SysUiState; import com.android.systemui.plugins.statusbar.StatusBarStateController; import com.android.systemui.recents.OverviewProxyService; @@ -70,6 +76,8 @@ import com.android.systemui.statusbar.policy.DeviceProvisionedController; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; @RunWith(AndroidTestingRunner.class) @RunWithLooper() @@ -85,6 +93,8 @@ public class NavigationBarFragmentTest extends SysuiBaseFragmentTest { private OverviewProxyService mOverviewProxyService; private CommandQueue mCommandQueue; private SysUiState mMockSysUiState; + @Mock + private BroadcastDispatcher mBroadcastDispatcher; private AccessibilityManagerWrapper mAccessibilityWrapper = new AccessibilityManagerWrapper(mContext) { @@ -112,6 +122,8 @@ public class NavigationBarFragmentTest extends SysuiBaseFragmentTest { @Before public void setupFragment() throws Exception { + MockitoAnnotations.initMocks(this); + setupSysuiDependency(); createRootView(); mOverviewProxyService = @@ -177,6 +189,18 @@ public class NavigationBarFragmentTest extends SysuiBaseFragmentTest { } @Test + public void testRegisteredWithDispatcher() { + mFragments.dispatchResume(); + processAllMessages(); + + verify(mBroadcastDispatcher).registerReceiver( + any(BroadcastReceiver.class), + any(IntentFilter.class), + any(Handler.class), + any(UserHandle.class)); + } + + @Test public void testSetImeWindowStatusWhenImeSwitchOnDisplay() { // Create default & external NavBar fragment. NavigationBarFragment defaultNavBar = (NavigationBarFragment) mFragment; @@ -227,7 +251,8 @@ public class NavigationBarFragmentTest extends SysuiBaseFragmentTest { mOverviewProxyService, mock(NavigationModeController.class), mock(StatusBarStateController.class), - mMockSysUiState); + mMockSysUiState, + mBroadcastDispatcher); } private class HostCallbacksForExternalDisplay extends diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java index 3be71c07009d..b75cb8cf487c 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/StatusBarTest.java @@ -41,7 +41,9 @@ import static org.mockito.Mockito.when; import android.app.Notification; import android.app.StatusBarManager; import android.app.trust.TrustManager; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.IntentFilter; import android.hardware.display.AmbientDisplayConfiguration; import android.hardware.fingerprint.FingerprintManager; import android.metrics.LogMaker; @@ -51,6 +53,7 @@ import android.os.IPowerManager; import android.os.Looper; import android.os.PowerManager; import android.os.RemoteException; +import android.os.UserHandle; import android.service.dreams.IDreamManager; import android.support.test.metricshelper.MetricsAsserts; import android.testing.AndroidTestingRunner; @@ -75,6 +78,7 @@ import com.android.systemui.SysuiTestCase; import com.android.systemui.UiOffloadThread; import com.android.systemui.appops.AppOpsController; import com.android.systemui.assist.AssistManager; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.bubbles.BubbleController; import com.android.systemui.doze.DozeHost; import com.android.systemui.doze.DozeLog; @@ -171,6 +175,8 @@ public class StatusBarTest extends SysuiTestCase { private AmbientDisplayConfiguration mAmbientDisplayConfiguration; @Mock private StatusBarWindowView mStatusBarWindowView; + @Mock + private BroadcastDispatcher mBroadcastDispatcher; private TestableStatusBar mStatusBar; private FakeMetricsLogger mMetricsLogger; @@ -199,6 +205,7 @@ public class StatusBarTest extends SysuiTestCase { mDependency.injectTestDependency(NotificationFilter.class, mNotificationFilter); mDependency.injectTestDependency(NotificationAlertingManager.class, mNotificationAlertingManager); + mDependency.injectTestDependency(BroadcastDispatcher.class, mBroadcastDispatcher); IPowerManager powerManagerService = mock(IPowerManager.class); mPowerManager = new PowerManager(mContext, powerManagerService, @@ -263,7 +270,8 @@ public class StatusBarTest extends SysuiTestCase { mDozeScrimController, mock(NotificationShelf.class), mLockscreenUserManager, mCommandQueue, mNotificationPresenter, mock(BubbleController.class), mock(NavigationBarController.class), - mock(AutoHideController.class), mKeyguardUpdateMonitor, mStatusBarWindowView); + mock(AutoHideController.class), mKeyguardUpdateMonitor, mStatusBarWindowView, + mBroadcastDispatcher); mStatusBar.mContext = mContext; mStatusBar.mComponents = mContext.getComponents(); SystemUIFactory.getInstance().getRootComponent() @@ -774,6 +782,16 @@ public class StatusBarTest extends SysuiTestCase { verify(mNotificationPanelView, never()).expand(anyBoolean()); } + @Test + public void testRegisterBroadcastsonDispatcher() { + mStatusBar.registerBroadcastReceiver(); + verify(mBroadcastDispatcher).registerReceiver( + any(BroadcastReceiver.class), + any(IntentFilter.class), + eq(null), + any(UserHandle.class)); + } + static class TestableStatusBar extends StatusBar { public TestableStatusBar(StatusBarKeyguardViewManager man, KeyguardIndicationController key, @@ -801,7 +819,8 @@ public class StatusBarTest extends SysuiTestCase { NavigationBarController navBarController, AutoHideController autoHideController, KeyguardUpdateMonitor keyguardUpdateMonitor, - StatusBarWindowView statusBarWindow) { + StatusBarWindowView statusBarWindow, + BroadcastDispatcher broadcastDispatcher) { mStatusBarKeyguardViewManager = man; mKeyguardIndicationController = key; mStackScroller = stack; @@ -835,6 +854,7 @@ public class StatusBarTest extends SysuiTestCase { mKeyguardUpdateMonitor = keyguardUpdateMonitor; mStatusBarWindow = statusBarWindow; mDozeServiceHost.mWakeLockScreenPerformsAuth = false; + mBroadcastDispatcher = broadcastDispatcher; } private WakefulnessLifecycle createAwakeWakefulnessLifecycle() { diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/SystemUIDialogTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/SystemUIDialogTest.java index a9a1392fb80b..589aa0353870 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/SystemUIDialogTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/SystemUIDialogTest.java @@ -18,11 +18,9 @@ import static junit.framework.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import android.content.BroadcastReceiver; -import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.testing.AndroidTestingRunner; @@ -31,11 +29,14 @@ import android.testing.TestableLooper.RunWithLooper; import androidx.test.filters.SmallTest; import com.android.systemui.SysuiTestCase; +import com.android.systemui.broadcast.BroadcastDispatcher; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; @RunWith(AndroidTestingRunner.class) @RunWithLooper @@ -43,13 +44,16 @@ import org.mockito.ArgumentCaptor; public class SystemUIDialogTest extends SysuiTestCase { private SystemUIDialog mDialog; - - Context mContextSpy; + @Mock + private BroadcastDispatcher mBroadcastDispatcher; @Before public void setup() { - mContextSpy = spy(mContext); - mDialog = new SystemUIDialog(mContextSpy); + MockitoAnnotations.initMocks(this); + + mDependency.injectTestDependency(BroadcastDispatcher.class, mBroadcastDispatcher); + + mDialog = new SystemUIDialog(mContext); } @Test @@ -60,12 +64,12 @@ public class SystemUIDialogTest extends SysuiTestCase { ArgumentCaptor.forClass(IntentFilter.class); mDialog.show(); - verify(mContextSpy).registerReceiverAsUser(broadcastReceiverCaptor.capture(), any(), - intentFilterCaptor.capture(), any(), any()); + verify(mBroadcastDispatcher).registerReceiver(broadcastReceiverCaptor.capture(), + intentFilterCaptor.capture(), eq(null), any()); assertTrue(intentFilterCaptor.getValue().hasAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); mDialog.dismiss(); - verify(mContextSpy).unregisterReceiver(eq(broadcastReceiverCaptor.getValue())); + verify(mBroadcastDispatcher).unregisterReceiver(eq(broadcastReceiverCaptor.getValue())); } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogControllerImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogControllerImplTest.java index f4d0854b2c9f..2e945f2481d2 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogControllerImplTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogControllerImplTest.java @@ -16,41 +16,64 @@ package com.android.systemui.volume; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import android.content.BroadcastReceiver; import android.content.Context; +import android.content.IntentFilter; import android.media.AudioManager; import android.media.session.MediaSession; +import android.os.Handler; +import android.testing.AndroidTestingRunner; import androidx.test.filters.SmallTest; import com.android.systemui.SysuiTestCase; +import com.android.systemui.broadcast.BroadcastDispatcher; import com.android.systemui.keyguard.WakefulnessLifecycle; import com.android.systemui.statusbar.phone.StatusBar; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +@RunWith(AndroidTestingRunner.class) @SmallTest public class VolumeDialogControllerImplTest extends SysuiTestCase { TestableVolumeDialogControllerImpl mVolumeController; VolumeDialogControllerImpl.C mCallback; StatusBar mStatusBar; + @Mock + private BroadcastDispatcher mBroadcastDispatcher; @Before public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + mCallback = mock(VolumeDialogControllerImpl.C.class); mStatusBar = mock(StatusBar.class); - mVolumeController = new TestableVolumeDialogControllerImpl(mContext, mCallback, mStatusBar); + mVolumeController = new TestableVolumeDialogControllerImpl(mContext, mCallback, mStatusBar, + mBroadcastDispatcher); mVolumeController.setEnableDialogs(true, true); } @Test + public void testRegisteredWithDispatcher() { + verify(mBroadcastDispatcher).registerReceiver( + any(BroadcastReceiver.class), + any(IntentFilter.class), + any(Handler.class)); // VolumeDialogControllerImpl does not call with user + } + + @Test public void testVolumeChangeW_deviceNotInteractiveAOD() { when(mStatusBar.isDeviceInteractive()).thenReturn(false); when(mStatusBar.getWakefulnessState()).thenReturn(WakefulnessLifecycle.WAKEFULNESS_AWAKE); @@ -81,7 +104,7 @@ public class VolumeDialogControllerImplTest extends SysuiTestCase { public void testVolumeChangeW_nullStatusBar() { VolumeDialogControllerImpl.C callback = mock(VolumeDialogControllerImpl.C.class); TestableVolumeDialogControllerImpl nullStatusBarTestableDialog = new - TestableVolumeDialogControllerImpl(mContext, callback, null); + TestableVolumeDialogControllerImpl(mContext, callback, null, mBroadcastDispatcher); nullStatusBarTestableDialog.setEnableDialogs(true, true); nullStatusBarTestableDialog.onVolumeChangedW(0, AudioManager.FLAG_SHOW_UI); verify(callback, times(1)).onShowRequested(Events.SHOW_REASON_VOLUME_CHANGED); @@ -100,8 +123,9 @@ public class VolumeDialogControllerImplTest extends SysuiTestCase { } static class TestableVolumeDialogControllerImpl extends VolumeDialogControllerImpl { - public TestableVolumeDialogControllerImpl(Context context, C callback, StatusBar s) { - super(context); + TestableVolumeDialogControllerImpl(Context context, C callback, StatusBar s, + BroadcastDispatcher broadcastDispatcher) { + super(context, broadcastDispatcher); mCallbacks = callback; mStatusBar = s; } diff --git a/proto/src/system_messages.proto b/proto/src/system_messages.proto index ffbf1ae38635..5d6d1c993cd3 100644 --- a/proto/src/system_messages.proto +++ b/proto/src/system_messages.proto @@ -230,6 +230,10 @@ message SystemMessage { // Package: android NOTE_TEST_HARNESS_MODE_ENABLED = 54; + // Inform the user that Serial Console is active. + // Package: android + NOTE_SERIAL_CONSOLE_ENABLED = 55; + // ADD_NEW_IDS_ABOVE_THIS_LINE // Legacy IDs with arbitrary values appear below // Legacy IDs existed as stable non-conflicting constants prior to the O release diff --git a/services/backup/backuplib/java/com/android/server/backup/TransportManager.java b/services/backup/backuplib/java/com/android/server/backup/TransportManager.java index 30ce4cf2fd3f..de48f4b13d7b 100644 --- a/services/backup/backuplib/java/com/android/server/backup/TransportManager.java +++ b/services/backup/backuplib/java/com/android/server/backup/TransportManager.java @@ -93,7 +93,8 @@ public class TransportManager { mTransportWhitelist = Preconditions.checkNotNull(whitelist); mCurrentTransportName = selectedTransport; mTransportStats = new TransportStats(); - mTransportClientManager = new TransportClientManager(mUserId, context, mTransportStats); + mTransportClientManager = TransportClientManager.createEncryptingClientManager(mUserId, + context, mTransportStats); } @VisibleForTesting diff --git a/services/backup/backuplib/java/com/android/server/backup/transport/TransportClientManager.java b/services/backup/backuplib/java/com/android/server/backup/transport/TransportClientManager.java index 8ce89b8d5c66..72b1ee741d95 100644 --- a/services/backup/backuplib/java/com/android/server/backup/transport/TransportClientManager.java +++ b/services/backup/backuplib/java/com/android/server/backup/transport/TransportClientManager.java @@ -19,6 +19,7 @@ package com.android.server.backup.transport; import static com.android.server.backup.TransportManager.SERVICE_ACTION_TRANSPORT_HOST; import static com.android.server.backup.transport.TransportUtils.formatMessage; +import android.annotation.Nullable; import android.annotation.UserIdInt; import android.content.ComponentName; import android.content.Context; @@ -32,6 +33,7 @@ import com.android.server.backup.transport.TransportUtils.Priority; import java.io.PrintWriter; import java.util.Map; import java.util.WeakHashMap; +import java.util.function.Function; /** * Manages the creation and disposal of {@link TransportClient}s. The only class that should use @@ -52,6 +54,7 @@ public class TransportClientManager { private final Object mTransportClientsLock = new Object(); private int mTransportClientsCreated = 0; private Map<TransportClient, String> mTransportClientsCallerMap = new WeakHashMap<>(); + private final Function<ComponentName, Intent> mIntentFunction; /** * Return an {@link Intent} which resolves to an intermediate {@link IBackupTransport} that @@ -65,6 +68,14 @@ public class TransportClientManager { } /** + * Return an {@link Intent} which resolves to the {@link IBackupTransport} for the {@link + * ComponentName}. + */ + private static Intent getRealTransportIntent(ComponentName transportComponent) { + return new Intent(SERVICE_ACTION_TRANSPORT_HOST).setComponent(transportComponent); + } + + /** * Given a {@link Intent} originally created by {@link * #getEncryptingTransportIntent(ComponentName)}, returns the {@link Intent} which resolves to * the {@link IBackupTransport} for that {@link ComponentName}. @@ -72,18 +83,35 @@ public class TransportClientManager { public static Intent getRealTransportIntent(Intent encryptingTransportIntent) { ComponentName transportComponent = encryptingTransportIntent.getParcelableExtra( ENCRYPTING_TRANSPORT_REAL_TRANSPORT_KEY); - Intent intent = new Intent(SERVICE_ACTION_TRANSPORT_HOST) - .setComponent(transportComponent) + Intent intent = getRealTransportIntent(transportComponent) .putExtras(encryptingTransportIntent.getExtras()); intent.removeExtra(ENCRYPTING_TRANSPORT_REAL_TRANSPORT_KEY); return intent; } + /** + * Create a {@link TransportClientManager} such that {@link #getTransportClient(ComponentName, + * Bundle, String)} returns a {@link TransportClient} which connects to an intermediate {@link + * IBackupTransport} that encrypts (or decrypts) the data when sending it (or receiving it) from + * the {@link IBackupTransport} for the given {@link ComponentName}. + */ + public static TransportClientManager createEncryptingClientManager(@UserIdInt int userId, + Context context, TransportStats transportStats) { + return new TransportClientManager(userId, context, transportStats, + TransportClientManager::getEncryptingTransportIntent); + } + public TransportClientManager(@UserIdInt int userId, Context context, TransportStats transportStats) { + this(userId, context, transportStats, TransportClientManager::getRealTransportIntent); + } + + private TransportClientManager(@UserIdInt int userId, Context context, + TransportStats transportStats, Function<ComponentName, Intent> intentFunction) { mUserId = userId; mContext = context; mTransportStats = transportStats; + mIntentFunction = intentFunction; } /** @@ -97,10 +125,7 @@ public class TransportClientManager { * @return A {@link TransportClient}. */ public TransportClient getTransportClient(ComponentName transportComponent, String caller) { - Intent bindIntent = - new Intent(SERVICE_ACTION_TRANSPORT_HOST).setComponent(transportComponent); - - return getTransportClient(transportComponent, caller, bindIntent); + return getTransportClient(transportComponent, null, caller); } /** @@ -115,11 +140,11 @@ public class TransportClientManager { * @return A {@link TransportClient}. */ public TransportClient getTransportClient( - ComponentName transportComponent, Bundle extras, String caller) { - Intent bindIntent = - new Intent(SERVICE_ACTION_TRANSPORT_HOST).setComponent(transportComponent); - bindIntent.putExtras(extras); - + ComponentName transportComponent, @Nullable Bundle extras, String caller) { + Intent bindIntent = mIntentFunction.apply(transportComponent); + if (extras != null) { + bindIntent.putExtras(extras); + } return getTransportClient(transportComponent, caller, bindIntent); } diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java index c07f67b902ac..7b69bea6014b 100644 --- a/services/core/java/com/android/server/am/ActivityManagerService.java +++ b/services/core/java/com/android/server/am/ActivityManagerService.java @@ -5320,10 +5320,42 @@ public class ActivityManagerService extends IActivityManager.Stub }); mUserController.scheduleStartProfiles(); } + // UART is on if init's console service is running, send a warning notification. + showConsoleNotificationIfActive(); t.traceEnd(); } + private void showConsoleNotificationIfActive() { + if (!SystemProperties.get("init.svc.console").equals("running")) { + return; + } + String title = mContext + .getString(com.android.internal.R.string.console_running_notification_title); + String message = mContext + .getString(com.android.internal.R.string.console_running_notification_message); + Notification notification = + new Notification.Builder(mContext, SystemNotificationChannels.DEVELOPER) + .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb) + .setWhen(0) + .setOngoing(true) + .setTicker(title) + .setDefaults(0) // please be quiet + .setColor(mContext.getColor( + com.android.internal.R.color + .system_notification_accent_color)) + .setContentTitle(title) + .setContentText(message) + .setVisibility(Notification.VISIBILITY_PUBLIC) + .build(); + + NotificationManager notificationManager = + mContext.getSystemService(NotificationManager.class); + notificationManager.notifyAsUser( + null, SystemMessage.NOTE_SERIAL_CONSOLE_ENABLED, notification, UserHandle.ALL); + + } + @Override public void bootAnimationComplete() { final boolean callFinishBooting; @@ -8279,6 +8311,8 @@ public class ActivityManagerService extends IActivityManager.Stub triggerShellBugreport.setAction(INTENT_BUGREPORT_REQUESTED); triggerShellBugreport.setPackage(SHELL_APP_PACKAGE); triggerShellBugreport.putExtra(EXTRA_BUGREPORT_TYPE, bugreportType); + triggerShellBugreport.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); + triggerShellBugreport.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); if (shareTitle != null) { triggerShellBugreport.putExtra(EXTRA_TITLE, shareTitle); } @@ -15267,7 +15301,7 @@ public class ActivityManagerService extends IActivityManager.Stub mBatteryStatsService.removeUid(uid); if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { mAppOpsService.resetAllModes(UserHandle.getUserId(uid), - intent.getData().getSchemeSpecificPart()); + intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME)); } else { mAppOpsService.uidRemoved(uid); } diff --git a/services/core/java/com/android/server/am/BroadcastQueue.java b/services/core/java/com/android/server/am/BroadcastQueue.java index 56208a9565e1..59c2326124f0 100644 --- a/services/core/java/com/android/server/am/BroadcastQueue.java +++ b/services/core/java/com/android/server/am/BroadcastQueue.java @@ -446,7 +446,9 @@ public final class BroadcastQueue { mHandler.removeCallbacksAndMessages(msgToken); // ...then schedule the removal of the token after the extended timeout mHandler.postAtTime(() -> { - app.removeAllowBackgroundActivityStartsToken(r); + synchronized (mService) { + app.removeAllowBackgroundActivityStartsToken(r); + } }, msgToken, (r.receiverTime + mConstants.ALLOW_BG_ACTIVITY_START_TIMEOUT)); } diff --git a/services/core/java/com/android/server/compat/PlatformCompat.java b/services/core/java/com/android/server/compat/PlatformCompat.java index 81e507cd24cf..8e09d0e5958e 100644 --- a/services/core/java/com/android/server/compat/PlatformCompat.java +++ b/services/core/java/com/android/server/compat/PlatformCompat.java @@ -18,6 +18,7 @@ package com.android.server.compat; import android.content.Context; import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; import android.util.Slog; import android.util.StatsLog; @@ -40,7 +41,8 @@ public class PlatformCompat extends IPlatformCompat.Stub { public PlatformCompat(Context context) { mContext = context; - mChangeReporter = new ChangeReporter(); + mChangeReporter = new ChangeReporter( + StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER); } @Override @@ -49,6 +51,15 @@ public class PlatformCompat extends IPlatformCompat.Stub { } @Override + public void reportChangeByPackageName(long changeId, String packageName) { + ApplicationInfo appInfo = getApplicationInfo(packageName); + if (appInfo == null) { + return; + } + reportChange(changeId, appInfo); + } + + @Override public boolean isChangeEnabled(long changeId, ApplicationInfo appInfo) { if (CompatConfig.get().isChangeEnabled(changeId, appInfo)) { reportChange(changeId, appInfo, @@ -61,17 +72,31 @@ public class PlatformCompat extends IPlatformCompat.Stub { } @Override + public boolean isChangeEnabledByPackageName(long changeId, String packageName) { + ApplicationInfo appInfo = getApplicationInfo(packageName); + if (appInfo == null) { + return true; + } + return isChangeEnabled(changeId, appInfo); + } + + @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, "platform_compat", pw)) return; CompatConfig.get().dumpConfig(pw); } + private ApplicationInfo getApplicationInfo(String packageName) { + try { + return mContext.getPackageManager().getApplicationInfo(packageName, 0); + } catch (PackageManager.NameNotFoundException e) { + Slog.e(TAG, "No installed package " + packageName); + } + return null; + } + private void reportChange(long changeId, ApplicationInfo appInfo, int state) { int uid = appInfo.uid; - //TODO(b/138374585): Implement rate limiting for the logs. - Slog.d(TAG, ChangeReporter.createLogString(uid, changeId, state)); - mChangeReporter.reportChange(uid, changeId, - state, /* source */ - StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER); + mChangeReporter.reportChange(uid, changeId, state); } } diff --git a/services/core/java/com/android/server/display/DisplayModeDirector.java b/services/core/java/com/android/server/display/DisplayModeDirector.java index 1fc0db3ff7cb..d24bd1aad1b1 100644 --- a/services/core/java/com/android/server/display/DisplayModeDirector.java +++ b/services/core/java/com/android/server/display/DisplayModeDirector.java @@ -49,8 +49,9 @@ import android.view.DisplayInfo; import com.android.internal.os.BackgroundThread; import com.android.internal.R; +import com.android.server.display.utils.AmbientFilter; +import com.android.server.display.utils.AmbientFilterFactory; import com.android.server.display.whitebalance.DisplayWhiteBalanceFactory; -import com.android.server.display.whitebalance.AmbientFilter; import java.io.PrintWriter; import java.util.ArrayList; @@ -970,7 +971,7 @@ public class DisplayModeDirector { if (lightSensor != null) { final Resources res = mContext.getResources(); - mAmbientFilter = DisplayWhiteBalanceFactory.createBrightnessFilter(res); + mAmbientFilter = AmbientFilterFactory.createBrightnessFilter(TAG, res); mLightSensor = lightSensor; onScreenOn(isDefaultDisplayOn()); diff --git a/services/core/java/com/android/server/display/whitebalance/AmbientFilter.java b/services/core/java/com/android/server/display/utils/AmbientFilter.java index 35808974b9e4..1a8412180c27 100644 --- a/services/core/java/com/android/server/display/whitebalance/AmbientFilter.java +++ b/services/core/java/com/android/server/display/utils/AmbientFilter.java @@ -14,13 +14,10 @@ * limitations under the License. */ -package com.android.server.display.whitebalance; +package com.android.server.display.utils; import android.util.Slog; -import com.android.internal.annotations.VisibleForTesting; -import com.android.server.display.utils.RollingBuffer; - import java.io.PrintWriter; import java.util.Arrays; diff --git a/services/core/java/com/android/server/display/utils/AmbientFilterFactory.java b/services/core/java/com/android/server/display/utils/AmbientFilterFactory.java new file mode 100644 index 000000000000..dfa1ddc67528 --- /dev/null +++ b/services/core/java/com/android/server/display/utils/AmbientFilterFactory.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2019 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.server.display.utils; + +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.util.TypedValue; + +public class AmbientFilterFactory { + /** + * Creates a temporal filter which functions as a weighted moving average buffer for recent + * sensor values. + * @param tag + * The tag used for dumping and logging. + * @param horizon + * How long ambient value changes are kept and taken into consideration. + * @param intercept + * Recent changes are prioritised by integrating their duration over y = x + intercept + * (the higher it is, the less prioritised recent changes are). + * + * @return + * An AmbientFiler. + * + * @throws IllegalArgumentException + * - Horizon is not positive. + * - Intercept not configured. + */ + public static AmbientFilter createAmbientFilter(String tag, int horizon, float intercept) { + if (!Float.isNaN(intercept)) { + return new AmbientFilter.WeightedMovingAverageAmbientFilter(tag, horizon, intercept); + } + throw new IllegalArgumentException("missing configurations: " + + "expected config_displayWhiteBalanceBrightnessFilterIntercept"); + } + + /** + * Helper to create a default BrightnessFilter which has configuration in the resource file. + * @param tag + * The tag used for dumping and logging. + * @param resources + * The resources used to configure the various components. + * + * @return + * An AmbientFilter. + */ + public static AmbientFilter createBrightnessFilter(String tag, Resources resources) { + final int horizon = resources.getInteger( + com.android.internal.R.integer.config_displayWhiteBalanceBrightnessFilterHorizon); + final float intercept = getFloat(resources, + com.android.internal.R.dimen.config_displayWhiteBalanceBrightnessFilterIntercept); + + return createAmbientFilter(tag, horizon, intercept); + } + + /** + * Helper to creates a default ColorTemperatureFilter which has configuration in the resource + * file. + * @param tag + * The tag used for dumping and logging. + * @param resources + * The resources used to configure the various components. + * + * @return + * An AmbientFilter. + */ + public static AmbientFilter createColorTemperatureFilter(String tag, Resources resources) { + final int horizon = resources.getInteger( + com.android.internal.R.integer + .config_displayWhiteBalanceColorTemperatureFilterHorizon); + final float intercept = getFloat(resources, + com.android.internal.R.dimen + .config_displayWhiteBalanceColorTemperatureFilterIntercept); + + return createAmbientFilter(tag, horizon, intercept); + } + + // Instantiation is disabled. + private AmbientFilterFactory() { } + + private static float getFloat(Resources resources, int id) { + TypedValue value = new TypedValue(); + + resources.getValue(id, value, true /* resolveRefs */); + if (value.type != TypedValue.TYPE_FLOAT) { + return Float.NaN; + } + + return value.getFloat(); + } +} + diff --git a/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceController.java b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceController.java index 7b1f4c3222f3..88a7077ac37a 100644 --- a/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceController.java +++ b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceController.java @@ -24,6 +24,7 @@ import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.Preconditions; import com.android.server.LocalServices; import com.android.server.display.color.ColorDisplayService.ColorDisplayServiceInternal; +import com.android.server.display.utils.AmbientFilter; import com.android.server.display.utils.History; import java.io.PrintWriter; @@ -63,7 +64,13 @@ public class DisplayWhiteBalanceController implements AmbientFilter mColorTemperatureFilter; private DisplayWhiteBalanceThrottler mThrottler; + // In low brightness conditions the ALS readings are more noisy and produce + // high errors. This default is introduced to provide a fixed display color + // temperature when sensor readings become unreliable. private final float mLowLightAmbientColorTemperature; + // In high brightness conditions certain color temperatures can cause peak display + // brightness to drop. This fixed color temperature can be used to compensate for + // this effect. private final float mHighLightAmbientColorTemperature; private float mAmbientColorTemperature; @@ -84,12 +91,14 @@ public class DisplayWhiteBalanceController implements // A piecewise linear relationship between ambient and display color temperatures. private Spline.LinearSpline mAmbientToDisplayColorTemperatureSpline; - // In very low or very high brightness conditions ambient EQ should to set to a default - // instead of using mAmbientToDisplayColorTemperatureSpline. However, setting ambient EQ - // based on thresholds can cause the display to rapidly change color temperature. To solve - // this, mLowLightAmbientBrightnessToBiasSpline and mHighLightAmbientBrightnessToBiasSpline - // are used to smoothly interpolate from ambient color temperature to the defaults. - // A piecewise linear relationship between low light brightness and low light bias. + // In very low or very high brightness conditions Display White Balance should + // be to set to a default instead of using mAmbientToDisplayColorTemperatureSpline. + // However, setting Display White Balance based on thresholds can cause the + // display to rapidly change color temperature. To solve this, + // mLowLightAmbientBrightnessToBiasSpline and + // mHighLightAmbientBrightnessToBiasSpline are used to smoothly interpolate from + // ambient color temperature to the defaults. A piecewise linear relationship + // between low light brightness and low light bias. private Spline.LinearSpline mLowLightAmbientBrightnessToBiasSpline; // A piecewise linear relationship between high light brightness and high light bias. diff --git a/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceFactory.java b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceFactory.java index bf0a1d16219d..a72b1ed8f3ec 100644 --- a/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceFactory.java +++ b/services/core/java/com/android/server/display/whitebalance/DisplayWhiteBalanceFactory.java @@ -23,6 +23,8 @@ import android.os.Handler; import android.util.TypedValue; import com.android.internal.annotations.VisibleForTesting; +import com.android.server.display.utils.AmbientFilter; +import com.android.server.display.utils.AmbientFilterFactory; /** * The DisplayWhiteBalanceFactory creates and configures an DisplayWhiteBalanceController. @@ -58,10 +60,12 @@ public class DisplayWhiteBalanceFactory { SensorManager sensorManager, Resources resources) { final AmbientSensor.AmbientBrightnessSensor brightnessSensor = createBrightnessSensor(handler, sensorManager, resources); - final AmbientFilter brightnessFilter = createBrightnessFilter(resources); + final AmbientFilter brightnessFilter = + AmbientFilterFactory.createBrightnessFilter(BRIGHTNESS_FILTER_TAG, resources); final AmbientSensor.AmbientColorTemperatureSensor colorTemperatureSensor = createColorTemperatureSensor(handler, sensorManager, resources); - final AmbientFilter colorTemperatureFilter = createColorTemperatureFilter(resources); + final AmbientFilter colorTemperatureFilter = AmbientFilterFactory + .createColorTemperatureFilter(COLOR_TEMPERATURE_FILTER_TAG, resources); final DisplayWhiteBalanceThrottler throttler = createThrottler(resources); final float[] displayWhiteBalanceLowLightAmbientBrightnesses = getFloatArray(resources, com.android.internal.R.array @@ -112,23 +116,6 @@ public class DisplayWhiteBalanceFactory { } /** - * Creates a BrightnessFilter which functions as a weighted moving average buffer for recent - * brightness values. - */ - public static AmbientFilter createBrightnessFilter(Resources resources) { - final int horizon = resources.getInteger( - com.android.internal.R.integer.config_displayWhiteBalanceBrightnessFilterHorizon); - final float intercept = getFloat(resources, - com.android.internal.R.dimen.config_displayWhiteBalanceBrightnessFilterIntercept); - if (!Float.isNaN(intercept)) { - return new AmbientFilter.WeightedMovingAverageAmbientFilter( - BRIGHTNESS_FILTER_TAG, horizon, intercept); - } - throw new IllegalArgumentException("missing configurations: " - + "expected config_displayWhiteBalanceBrightnessFilterIntercept"); - } - - /** * Creates an ambient color sensor instance to redirect sensor data to callbacks. */ @VisibleForTesting @@ -143,21 +130,6 @@ public class DisplayWhiteBalanceFactory { return new AmbientSensor.AmbientColorTemperatureSensor(handler, sensorManager, name, rate); } - private static AmbientFilter createColorTemperatureFilter(Resources resources) { - final int horizon = resources.getInteger( - com.android.internal.R.integer - .config_displayWhiteBalanceColorTemperatureFilterHorizon); - final float intercept = getFloat(resources, - com.android.internal.R.dimen - .config_displayWhiteBalanceColorTemperatureFilterIntercept); - if (!Float.isNaN(intercept)) { - return new AmbientFilter.WeightedMovingAverageAmbientFilter( - COLOR_TEMPERATURE_FILTER_TAG, horizon, intercept); - } - throw new IllegalArgumentException("missing configurations: " - + "expected config_displayWhiteBalanceColorTemperatureFilterIntercept"); - } - private static DisplayWhiteBalanceThrottler createThrottler(Resources resources) { final int increaseDebounce = resources.getInteger( com.android.internal.R.integer.config_displayWhiteBalanceDecreaseDebounce); diff --git a/services/core/java/com/android/server/media/MediaSessionRecord.java b/services/core/java/com/android/server/media/MediaSessionRecord.java index 248351ca3d2f..0aee8507d5af 100644 --- a/services/core/java/com/android/server/media/MediaSessionRecord.java +++ b/services/core/java/com/android/server/media/MediaSessionRecord.java @@ -456,6 +456,7 @@ public class MediaSessionRecord implements IBinder.DeathRecipient { return; } mDestroyed = true; + mPlaybackState = null; mHandler.post(MessageHandler.MSG_DESTROYED); } } diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java index 123e65e5b9ac..d07e2d232ea6 100644 --- a/services/core/java/com/android/server/pm/PackageManagerService.java +++ b/services/core/java/com/android/server/pm/PackageManagerService.java @@ -137,7 +137,6 @@ import android.content.IntentSender; import android.content.IntentSender.SendIntentException; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; -import android.content.pm.AppsQueryHelper; import android.content.pm.AuxiliaryResolveInfo; import android.content.pm.ChangedPackages; import android.content.pm.ComponentInfo; @@ -2473,54 +2472,21 @@ public class PackageManagerService extends IPackageManager.Stub PackageManagerService m = new PackageManagerService(injector, factoryTest, onlyCore); t.traceEnd(); // "create package manager" - m.enableSystemUserPackages(); + m.installWhitelistedSystemPackages(); ServiceManager.addService("package", m); final PackageManagerNative pmn = m.new PackageManagerNative(); ServiceManager.addService("package_native", pmn); return m; } - private void enableSystemUserPackages() { - if (!UserManager.isSplitSystemUser()) { - return; - } - // For system user, enable apps based on the following conditions: - // - app is whitelisted or belong to one of these groups: - // -- system app which has no launcher icons - // -- system app which has INTERACT_ACROSS_USERS permission - // -- system IME app - // - app is not in the blacklist - AppsQueryHelper queryHelper = new AppsQueryHelper(this); - Set<String> enableApps = new ArraySet<>(); - enableApps.addAll(queryHelper.queryApps(AppsQueryHelper.GET_NON_LAUNCHABLE_APPS - | AppsQueryHelper.GET_APPS_WITH_INTERACT_ACROSS_USERS_PERM - | AppsQueryHelper.GET_IMES, /* systemAppsOnly */ true, UserHandle.SYSTEM)); - ArraySet<String> wlApps = SystemConfig.getInstance().getSystemUserWhitelistedApps(); - enableApps.addAll(wlApps); - enableApps.addAll(queryHelper.queryApps(AppsQueryHelper.GET_REQUIRED_FOR_SYSTEM_USER, - /* systemAppsOnly */ false, UserHandle.SYSTEM)); - ArraySet<String> blApps = SystemConfig.getInstance().getSystemUserBlacklistedApps(); - enableApps.removeAll(blApps); - Log.i(TAG, "Applications installed for system user: " + enableApps); - List<String> allAps = queryHelper.queryApps(0, /* systemAppsOnly */ false, - UserHandle.SYSTEM); - final int allAppsSize = allAps.size(); + /** Install/uninstall system packages for all users based on their user-type, as applicable. */ + private void installWhitelistedSystemPackages() { synchronized (mLock) { - for (int i = 0; i < allAppsSize; i++) { - String pName = allAps.get(i); - PackageSetting pkgSetting = mSettings.mPackages.get(pName); - // Should not happen, but we shouldn't be failing if it does - if (pkgSetting == null) { - continue; - } - boolean install = enableApps.contains(pName); - if (pkgSetting.getInstalled(UserHandle.USER_SYSTEM) != install) { - Log.i(TAG, (install ? "Installing " : "Uninstalling ") + pName - + " for system user"); - pkgSetting.setInstalled(install, UserHandle.USER_SYSTEM); - } + final boolean scheduleWrite = mUserManager.installWhitelistedSystemPackages( + isFirstBoot(), isDeviceUpgrading()); + if (scheduleWrite) { + scheduleWritePackageRestrictionsLocked(UserHandle.USER_ALL); } - scheduleWritePackageRestrictionsLocked(UserHandle.USER_SYSTEM); } } @@ -17746,8 +17712,14 @@ public class PackageManagerService extends IPackageManager.Stub } } if (removedAppId >= 0) { + // If a system app's updates are uninstalled the UID is not actually removed. Some + // services need to know the package name affected. + if (extras.getBoolean(Intent.EXTRA_REPLACING, false)) { + extras.putString(Intent.EXTRA_PACKAGE_NAME, removedPackage); + } + packageSender.sendPackageBroadcast(Intent.ACTION_UID_REMOVED, - removedPackage, extras, Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND, + null, extras, Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND, null, null, broadcastUsers, instantUserIds); } } @@ -22312,10 +22284,19 @@ public class PackageManagerService extends IPackageManager.Stub } } - /** Called by UserManagerService */ - void createNewUser(int userId, String[] disallowedPackages) { + /** + * Called by UserManagerService. + * + * @param installablePackages system packages that should be initially installed for this user, + * or {@code null} if all system packages should be installed + * @param disallowedPackages packages that should not be initially installed. Takes precedence + * over installablePackages. + */ + void createNewUser(int userId, @Nullable Set<String> installablePackages, + String[] disallowedPackages) { synchronized (mInstallLock) { - mSettings.createNewUserLI(this, mInstaller, userId, disallowedPackages); + mSettings.createNewUserLI(this, mInstaller, userId, + installablePackages, disallowedPackages); } synchronized (mLock) { scheduleWritePackageRestrictionsLocked(userId); @@ -23124,6 +23105,19 @@ public class PackageManagerService extends IPackageManager.Stub } @Override + public boolean setInstalled(PackageParser.Package pkg, @UserIdInt int userId, + boolean installed) { + synchronized (mLock) { + final PackageSetting ps = mSettings.mPackages.get(pkg.packageName); + if (ps.getInstalled(userId) != installed) { + ps.setInstalled(installed, userId); + return true; + } + return false; + } + } + + @Override public void requestInstantAppResolutionPhaseTwo(AuxiliaryResolveInfo responseObj, Intent origIntent, String resolvedType, String callingPackage, Bundle verificationBundle, int userId) { diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java index fe529a152364..3f32f3dde3ae 100644 --- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java +++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java @@ -496,6 +496,10 @@ class PackageManagerShellCommand extends ShellCommand { getErrPrintWriter().println("Error: no package specified"); return 1; } + userId = translateUserId(userId, true /*allowAll*/, "runPath"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } return displayPackageFilePath(pkg, userId); } @@ -718,6 +722,10 @@ class PackageManagerShellCommand extends ShellCommand { final String filter = getNextArg(); + userId = translateUserId(userId, true /*allowAll*/, "runListPackages"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } @SuppressWarnings("unchecked") final ParceledListSlice<PackageInfo> slice = mInterface.getInstalledPackages(getFlags, userId); @@ -1285,7 +1293,7 @@ class PackageManagerShellCommand extends ShellCommand { private int runInstallExisting() throws RemoteException { final PrintWriter pw = getOutPrintWriter(); - int userId = UserHandle.USER_SYSTEM; + int userId = UserHandle.USER_CURRENT; int installFlags = PackageManager.INSTALL_ALL_WHITELIST_RESTRICTED_PERMISSIONS; String opt; boolean waitTillComplete = false; @@ -1320,6 +1328,10 @@ class PackageManagerShellCommand extends ShellCommand { pw.println("Error: package name not specified"); return 1; } + userId = translateUserId(userId, true /*allowAll*/, "runInstallExisting"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } int installReason = PackageManager.INSTALL_REASON_UNKNOWN; try { @@ -1945,6 +1957,10 @@ class PackageManagerShellCommand extends ShellCommand { getErrPrintWriter().println("Error: no package or component specified"); return 1; } + userId = translateUserId(userId, true /*allowAll*/, "runSetEnabledSetting"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } ComponentName cn = ComponentName.unflattenFromString(pkg); if (cn == null) { mInterface.setApplicationEnabledSetting(pkg, state, 0, userId, @@ -1974,6 +1990,10 @@ class PackageManagerShellCommand extends ShellCommand { getErrPrintWriter().println("Error: no package or component specified"); return 1; } + userId = translateUserId(userId, true /*allowAll*/, "runSetHiddenSetting"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } mInterface.setApplicationHiddenSettingAsUser(pkg, state, userId); getOutPrintWriter().println("Package " + pkg + " new hidden state: " + mInterface.getApplicationHiddenSettingAsUser(pkg, userId)); @@ -2043,6 +2063,10 @@ class PackageManagerShellCommand extends ShellCommand { info = null; } try { + userId = translateUserId(userId, true /*allowAll*/, "runSuspend"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } mInterface.setPackagesSuspendedAsUser(new String[]{packageName}, suspendedState, appExtras, launcherExtras, info, callingPackage, userId); pw.println("Package " + packageName + " new suspended state: " @@ -2074,7 +2098,7 @@ class PackageManagerShellCommand extends ShellCommand { getErrPrintWriter().println("Error: no permission specified"); return 1; } - + userId = translateUserId(userId, true /*allowAll*/, "runGrantRevokePermission"); if (grant) { mPermissionManager.grantRuntimePermission(pkg, perm, userId); } else { @@ -2262,6 +2286,10 @@ class PackageManagerShellCommand extends ShellCommand { return 1; } + userId = translateUserId(userId, true /*allowAll*/, "runSetAppLink"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } final PackageInfo info = mInterface.getPackageInfo(pkg, 0, userId); if (info == null) { getErrPrintWriter().println("Error: package " + pkg + " not found."); @@ -2302,6 +2330,10 @@ class PackageManagerShellCommand extends ShellCommand { return 1; } + userId = translateUserId(userId, true /*allowAll*/, "runGetAppLink"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } final PackageInfo info = mInterface.getPackageInfo(pkg, 0, userId); if (info == null) { getErrPrintWriter().println("Error: package " + pkg + " not found."); @@ -2666,8 +2698,7 @@ class PackageManagerShellCommand extends ShellCommand { } pkgName = componentName.getPackageName(); } - - + userId = translateUserId(userId, true /*allowAll*/, "runInstallCreate"); final CompletableFuture<Boolean> future = new CompletableFuture<>(); final RemoteCallback callback = new RemoteCallback(res -> future.complete(res != null)); try { @@ -2763,8 +2794,10 @@ class PackageManagerShellCommand extends ShellCommand { } } - userId = translateUserId(userId, false /*allowAll*/, "runSetHarmfulAppWarning"); - + userId = translateUserId(userId, true /*allowAll*/, "runSetHarmfulAppWarning"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } final String packageName = getNextArgRequired(); final String warning = getNextArg(); @@ -2786,8 +2819,10 @@ class PackageManagerShellCommand extends ShellCommand { } } - userId = translateUserId(userId, false /*allowAll*/, "runGetHarmfulAppWarning"); - + userId = translateUserId(userId, true /*allowAll*/, "runGetHarmfulAppWarning"); + if (userId == UserHandle.USER_ALL) { + userId = UserHandle.USER_SYSTEM; + } final String packageName = getNextArgRequired(); final CharSequence warning = mInterface.getHarmfulAppWarning(packageName, userId); if (!TextUtils.isEmpty(warning)) { @@ -2824,7 +2859,7 @@ class PackageManagerShellCommand extends ShellCommand { private int doCreateSession(SessionParams params, String installerPackageName, int userId) throws RemoteException { - userId = translateUserId(userId, true /*allowAll*/, "runInstallCreate"); + userId = translateUserId(userId, true /*allowAll*/, "doCreateSession"); if (userId == UserHandle.USER_ALL) { userId = UserHandle.USER_SYSTEM; params.installFlags |= PackageManager.INSTALL_ALL_USERS; @@ -3115,13 +3150,13 @@ class PackageManagerShellCommand extends ShellCommand { pw.println(" dump PACKAGE"); pw.println(" Print various system state associated with the given PACKAGE."); pw.println(""); - pw.println(" list features"); - pw.println(" Prints all features of the system."); - pw.println(""); pw.println(" has-feature FEATURE_NAME [version]"); pw.println(" Prints true and returns exit status 0 when system has a FEATURE_NAME,"); pw.println(" otherwise prints false and returns exit status 1"); pw.println(""); + pw.println(" list features"); + pw.println(" Prints all features of the system."); + pw.println(""); pw.println(" list instrumentation [-f] [TARGET-PACKAGE]"); pw.println(" Prints all test packages; optionally only those targeting TARGET-PACKAGE"); pw.println(" Options:"); @@ -3161,11 +3196,14 @@ class PackageManagerShellCommand extends ShellCommand { pw.println(" -u: list only the permissions users will see"); pw.println(""); pw.println(" list staged-sessions [--only-ready] [--only-sessionid] [--only-parent]"); - pw.println(" Displays list of all staged sessions on device."); + pw.println(" Prints all staged sessions."); pw.println(" --only-ready: show only staged sessions that are ready"); pw.println(" --only-sessionid: show only sessionId of each session"); pw.println(" --only-parent: hide all children sessions"); pw.println(""); + pw.println(" list users"); + pw.println(" Prints all users."); + pw.println(""); pw.println(" resolve-activity [--brief] [--components] [--query-flags FLAGS]"); pw.println(" [--user USER_ID] INTENT"); pw.println(" Prints the activity that resolves to the given INTENT."); @@ -3186,7 +3224,7 @@ class PackageManagerShellCommand extends ShellCommand { pw.println(" [-p INHERIT_PACKAGE] [--install-location 0/1/2]"); pw.println(" [--install-reason 0/1/2/3/4] [--originating-uri URI]"); pw.println(" [--referrer URI] [--abi ABI_NAME] [--force-sdk]"); - pw.println(" [--preload] [--instantapp] [--full] [--dont-kill]"); + pw.println(" [--preload] [--instant] [--full] [--dont-kill]"); pw.println(" [--enable-rollback]"); pw.println(" [--force-uuid internal|UUID] [--pkg PACKAGE] [-S BYTES]"); pw.println(" [--apex] [--wait TIMEOUT]"); @@ -3209,7 +3247,7 @@ class PackageManagerShellCommand extends ShellCommand { pw.println(" --referrer: set URI that instigated the install of the app"); pw.println(" --pkg: specify expected package name of app being installed"); pw.println(" --abi: override the default ABI of the platform"); - pw.println(" --instantapp: cause the app to be installed as an ephemeral install app"); + pw.println(" --instant: cause the app to be installed as an ephemeral install app"); pw.println(" --full: cause the app to be installed as a non-ephemeral full app"); pw.println(" --install-location: force the install location:"); pw.println(" 0=auto, 1=internal only, 2=prefer external"); @@ -3222,11 +3260,20 @@ class PackageManagerShellCommand extends ShellCommand { pw.println(" for pre-reboot verification to complete. If TIMEOUT is not"); pw.println(" specified it will wait for " + DEFAULT_WAIT_MS + " milliseconds."); pw.println(""); + pw.println(" install-existing [--user USER_ID|all|current]"); + pw.println(" [--instant] [--full] [--wait] [--restrict-permissions] PACKAGE"); + pw.println(" Installs an existing application for a new user. Options are:"); + pw.println(" --user: install for the given user."); + pw.println(" --instant: install as an instant app"); + pw.println(" --full: install as a full app"); + pw.println(" --wait: wait until the package is installed"); + pw.println(" --restrict-permissions: don't whitelist restricted permissions"); + pw.println(""); pw.println(" install-create [-lrtsfdg] [-i PACKAGE] [--user USER_ID|all|current]"); pw.println(" [-p INHERIT_PACKAGE] [--install-location 0/1/2]"); pw.println(" [--install-reason 0/1/2/3/4] [--originating-uri URI]"); pw.println(" [--referrer URI] [--abi ABI_NAME] [--force-sdk]"); - pw.println(" [--preload] [--instantapp] [--full] [--dont-kill]"); + pw.println(" [--preload] [--instant] [--full] [--dont-kill]"); pw.println(" [--force-uuid internal|UUID] [--pkg PACKAGE] [--apex] [-S BYTES]"); pw.println(" [--multi-package] [--staged]"); pw.println(" Like \"install\", but starts an install session. Use \"install-write\""); diff --git a/services/core/java/com/android/server/pm/Settings.java b/services/core/java/com/android/server/pm/Settings.java index 1873a4ec98d9..4349ea75809c 100644 --- a/services/core/java/com/android/server/pm/Settings.java +++ b/services/core/java/com/android/server/pm/Settings.java @@ -4014,8 +4014,9 @@ public final class Settings { } } - void createNewUserLI(@NonNull PackageManagerService service, - @NonNull Installer installer, int userHandle, String[] disallowedPackages) { + void createNewUserLI(@NonNull PackageManagerService service, @NonNull Installer installer, + @UserIdInt int userHandle, @Nullable Set<String> installablePackages, + String[] disallowedPackages) { final TimingsTraceAndSlog t = new TimingsTraceAndSlog(TAG + "Timing", Trace.TRACE_TAG_PACKAGE_MANAGER); t.traceBegin("createNewUser-" + userHandle); @@ -4025,6 +4026,7 @@ public final class Settings { String[] seinfos; int[] targetSdkVersions; int packagesCount; + final boolean skipPackageWhitelist = installablePackages == null; synchronized (mLock) { Collection<PackageSetting> packages = mPackages.values(); packagesCount = packages.size(); @@ -4040,6 +4042,7 @@ public final class Settings { continue; } final boolean shouldInstall = ps.isSystem() && + (skipPackageWhitelist || installablePackages.contains(ps.name)) && !ArrayUtils.contains(disallowedPackages, ps.name) && !ps.pkg.applicationInfo.hiddenUntilInstalled; // Only system apps are initially installed. diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java index 9371c4473bb3..5f8670809bfb 100644 --- a/services/core/java/com/android/server/pm/UserManagerService.java +++ b/services/core/java/com/android/server/pm/UserManagerService.java @@ -250,6 +250,9 @@ public class UserManagerService extends IUserManager.Stub { private static final IBinder mUserRestriconToken = new Binder(); + /** Installs system packages based on user-type. */ + private final UserSystemPackageInstaller mSystemPackageInstaller; + /** * Internal non-parcelable wrapper for UserInfo that is not exposed to other system apps. */ @@ -550,6 +553,7 @@ public class UserManagerService extends IUserManager.Stub { readUserListLP(); sInstance = this; } + mSystemPackageInstaller = new UserSystemPackageInstaller(this); mLocalService = new LocalService(); LocalServices.addService(UserManagerInternal.class, mLocalService); mLockPatternUtils = new LockPatternUtils(mContext); @@ -2842,8 +2846,10 @@ public class UserManagerService extends IUserManager.Stub { StorageManager.FLAG_STORAGE_DE | StorageManager.FLAG_STORAGE_CE); t.traceEnd(); + final Set<String> installablePackages = + mSystemPackageInstaller.getInstallablePackagesForUserType(flags); t.traceBegin("PM.createNewUser"); - mPm.createNewUser(userId, disallowedPackages); + mPm.createNewUser(userId, installablePackages, disallowedPackages); t.traceEnd(); userInfo.partial = false; @@ -2877,6 +2883,11 @@ public class UserManagerService extends IUserManager.Stub { return userInfo; } + /** Install/uninstall system packages for all users based on their user-type, as applicable. */ + boolean installWhitelistedSystemPackages(boolean isFirstBoot, boolean isUpgrade) { + return mSystemPackageInstaller.installWhitelistedSystemPackages(isFirstBoot, isUpgrade); + } + @VisibleForTesting UserData putUserInfo(UserInfo userInfo) { final UserData userData = new UserData(); @@ -3863,6 +3874,10 @@ public class UserManagerService extends IUserManager.Stub { pw.println(" Is split-system user: " + UserManager.isSplitSystemUser()); pw.println(" Is headless-system mode: " + UserManager.isHeadlessSystemUserMode()); pw.println(" User version: " + mUserVersion); + + // Dump package whitelist + pw.println(); + mSystemPackageInstaller.dump(pw); } private static void dumpTimeAgo(PrintWriter pw, StringBuilder sb, long nowTime, long time) { diff --git a/services/core/java/com/android/server/pm/UserSystemPackageInstaller.java b/services/core/java/com/android/server/pm/UserSystemPackageInstaller.java new file mode 100644 index 000000000000..036d1e807f97 --- /dev/null +++ b/services/core/java/com/android/server/pm/UserSystemPackageInstaller.java @@ -0,0 +1,459 @@ +/* + * Copyright (C) 2019 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.server.pm; + +import android.annotation.IntDef; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.annotation.UserIdInt; +import android.content.pm.PackageManagerInternal; +import android.content.pm.PackageParser; +import android.content.pm.UserInfo; +import android.content.res.Resources; +import android.os.SystemProperties; +import android.os.UserHandle; +import android.util.ArrayMap; +import android.util.ArraySet; +import android.util.Slog; + +import com.android.internal.annotations.VisibleForTesting; +import com.android.server.LocalServices; +import com.android.server.SystemConfig; + +import java.io.PrintWriter; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.Set; + +/** + * Responsible for un/installing system packages based on user type. + * + * <p>Uses the SystemConfig's install-in-user-type whitelist; + * see {@link SystemConfig#getAndClearPackageToUserTypeWhitelist} and + * {@link SystemConfig#getAndClearPackageToUserTypeBlacklist}. + * + * <p>If {@link #isEnforceMode()} is false, then all system packages are always installed for all + * users. The following applies when it is true. + * + * Any package can be in one of three states in the SystemConfig whitelist + * <ol> + * <li>Explicitly blacklisted for a particular user type</li> + * <li>Explicitly whitelisted for a particular user type</li> + * <li>Not mentioned at all, for any user type (neither whitelisted nor blacklisted)</li> + * </ol> + * Blacklisting always takes precedence - if a package is blacklisted for a particular user, + * it won't be installed on that type of user (even if it is also whitelisted for that user). + * Next comes whitelisting - if it is whitelisted for a particular user, it will be installed on + * that type of user (as long as it isn't blacklisted). + * Finally, if the package is not mentioned at all (i.e. neither whitelisted nor blacklisted for + * any user types) in the SystemConfig 'install-in-user-type' lists + * then: + * <ul> + * <li>If {@link #isImplicitWhitelistMode()}, the package is implicitly treated as whitelisted + * for all users</li> + * <li>Otherwise, the package is implicitly treated as blacklisted for all non-SYSTEM users</li> + * <li>Either way, for {@link UserHandle#USER_SYSTEM}, the package will be implicitly + * whitelisted so that it can be used for local development purposes.</li> + * </ul> + */ +class UserSystemPackageInstaller { + private static final String TAG = "UserManagerService"; + + /** + * System Property whether to only install system packages on a user if they're whitelisted for + * that user type. These are flags and can be freely combined. + * <ul> + * <li> 0 (0b000) - disable whitelist (install all system packages; no logging)</li> + * <li> 1 (0b001) - enforce (only install system packages if they are whitelisted)</li> + * <li> 2 (0b010) - log (log when a non-whitelisted package is run)</li> + * <li> 4 (0b100) - implicitly whitelist any package not mentioned in the whitelist</li> + * <li>-1 - use device default (as defined in res/res/values/config.xml)</li> + * </ul> + * Note: This list must be kept current with config_userTypePackageWhitelistMode in + * frameworks/base/core/res/res/values/config.xml + */ + static final String PACKAGE_WHITELIST_MODE_PROP = "persist.debug.user.package_whitelist_mode"; + static final int USER_TYPE_PACKAGE_WHITELIST_MODE_DISABLE = 0; + static final int USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE = 0b001; + static final int USER_TYPE_PACKAGE_WHITELIST_MODE_LOG = 0b010; + static final int USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST = 0b100; + static final int USER_TYPE_PACKAGE_WHITELIST_MODE_DEVICE_DEFAULT = -1; + + @IntDef(flag = true, prefix = "USER_TYPE_PACKAGE_WHITELIST_MODE_", value = { + USER_TYPE_PACKAGE_WHITELIST_MODE_DISABLE, + USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE, + USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE, + USER_TYPE_PACKAGE_WHITELIST_MODE_LOG, + USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST, + }) + @Retention(RetentionPolicy.SOURCE) + public @interface PackageWhitelistMode {} + + /** + * Maps system package manifest names to the user flags on which they should be initially + * installed. + * <p>Packages that are whitelisted, but then blacklisted so that they aren't to be installed on + * any user, are purposefully still present in this list. + */ + private final ArrayMap<String, Integer> mWhitelitsedPackagesForUserTypes; + + private final UserManagerService mUm; + + UserSystemPackageInstaller(UserManagerService ums) { + mUm = ums; + mWhitelitsedPackagesForUserTypes = + determineWhitelistedPackagesForUserTypes(SystemConfig.getInstance()); + } + + /** Constructor for testing purposes. */ + @VisibleForTesting + UserSystemPackageInstaller(UserManagerService ums, ArrayMap<String, Integer> whitelist) { + mUm = ums; + mWhitelitsedPackagesForUserTypes = whitelist; + } + + /** + * During OTAs and first boot, install/uninstall all system packages for all users based on the + * user's UserInfo flags and the SystemConfig whitelist. + * We do NOT uninstall packages during an OTA though. + * + * This is responsible for enforcing the whitelist for pre-existing users (i.e. USER_SYSTEM); + * enforcement for new users is done when they are created in UserManagerService.createUser(). + */ + boolean installWhitelistedSystemPackages(boolean isFirstBoot, boolean isUpgrade) { + final int mode = getWhitelistMode(); + checkWhitelistedSystemPackages(mode); + if (!isUpgrade && !isFirstBoot) { + return false; + } + Slog.i(TAG, "Reviewing whitelisted packages due to " + + (isFirstBoot ? "[firstBoot]" : "") + (isUpgrade ? "[upgrade]" : "")); + final PackageManagerInternal pmInt = LocalServices.getService(PackageManagerInternal.class); + // Install/uninstall system packages per user. + for (int userId : mUm.getUserIds()) { + final Set<String> userWhitelist = getInstallablePackagesForUserId(userId); + pmInt.forEachPackage(pkg -> { + if (!pkg.isSystem()) { + return; + } + final boolean install = + (userWhitelist == null || userWhitelist.contains(pkg.packageName)) + && !pkg.applicationInfo.hiddenUntilInstalled; + if (isUpgrade && !isFirstBoot && !install) { + return; // To be careful, we don’t uninstall apps during OTAs + } + final boolean changed = pmInt.setInstalled(pkg, userId, install); + if (changed) { + Slog.i(TAG, (install ? "Installed " : "Uninstalled ") + + pkg.packageName + " for user " + userId); + } + }); + } + return true; + } + + /** + * Checks whether the system packages and the mWhitelistedPackagesForUserTypes whitelist are + * in 1-to-1 correspondence. + */ + private void checkWhitelistedSystemPackages(@PackageWhitelistMode int mode) { + if (!isLogMode(mode) && !isEnforceMode(mode)) { + return; + } + Slog.v(TAG, "Checking that all system packages are whitelisted."); + final Set<String> allWhitelistedPackages = getWhitelistedSystemPackages(); + PackageManagerInternal pmInt = LocalServices.getService(PackageManagerInternal.class); + + // Check whether all whitelisted packages are indeed on the system. + for (String pkgName : allWhitelistedPackages) { + PackageParser.Package pkg = pmInt.getPackage(pkgName); + if (pkg == null) { + Slog.w(TAG, pkgName + " is whitelisted but not present."); + } else if (!pkg.isSystem()) { + Slog.w(TAG, pkgName + " is whitelisted and present but not a system package."); + } + } + + // Check whether all system packages are indeed whitelisted. + if (isImplicitWhitelistMode(mode) && !isLogMode(mode)) { + return; + } + final boolean doWtf = isEnforceMode(mode); + pmInt.forEachPackage(pkg -> { + if (pkg.isSystem() && !allWhitelistedPackages.contains(pkg.manifestPackageName)) { + final String msg = "System package " + pkg.manifestPackageName + + " is not whitelisted using 'install-in-user-type' in SystemConfig " + + "for any user types!"; + if (doWtf) { + Slog.wtf(TAG, msg); + } else { + Slog.e(TAG, msg); + } + } + }); + } + + /** Whether to only install system packages in new users for which they are whitelisted. */ + boolean isEnforceMode() { + return isEnforceMode(getWhitelistMode()); + } + + /** + * Whether to log a warning concerning potential problems with the user-type package whitelist. + */ + boolean isLogMode() { + return isLogMode(getWhitelistMode()); + } + + /** + * Whether to treat all packages that are not mentioned at all in the whitelist to be implicitly + * whitelisted for all users. + */ + boolean isImplicitWhitelistMode() { + return isImplicitWhitelistMode(getWhitelistMode()); + } + + /** See {@link #isEnforceMode()}. */ + private static boolean isEnforceMode(int whitelistMode) { + return (whitelistMode & USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE) != 0; + } + + /** See {@link #isLogMode()}. */ + private static boolean isLogMode(int whitelistMode) { + return (whitelistMode & USER_TYPE_PACKAGE_WHITELIST_MODE_LOG) != 0; + } + + /** See {@link #isImplicitWhitelistMode()}. */ + private static boolean isImplicitWhitelistMode(int whitelistMode) { + return (whitelistMode & USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST) != 0; + } + + /** Gets the PackageWhitelistMode for use of {@link #mWhitelitsedPackagesForUserTypes}. */ + private @PackageWhitelistMode int getWhitelistMode() { + final int runtimeMode = SystemProperties.getInt( + PACKAGE_WHITELIST_MODE_PROP, USER_TYPE_PACKAGE_WHITELIST_MODE_DEVICE_DEFAULT); + if (runtimeMode != USER_TYPE_PACKAGE_WHITELIST_MODE_DEVICE_DEFAULT) { + return runtimeMode; + } + return Resources.getSystem() + .getInteger(com.android.internal.R.integer.config_userTypePackageWhitelistMode); + } + + /** + * Gets the system packages names that should be installed on the given user. + * See {@link #getInstallablePackagesForUserType(int)}. + */ + private @Nullable Set<String> getInstallablePackagesForUserId(@UserIdInt int userId) { + return getInstallablePackagesForUserType(mUm.getUserInfo(userId).flags); + } + + /** + * Gets the system package names that should be installed on a user with the given flags, as + * determined by SystemConfig, the whitelist mode, and the apps actually on the device. + * Names are the {@link PackageParser.Package#packageName}, not necessarily the manifest names. + * + * Returns null if all system packages should be installed (due enforce-mode being off). + */ + @Nullable Set<String> getInstallablePackagesForUserType(int flags) { + final int mode = getWhitelistMode(); + if (!isEnforceMode(mode)) { + return null; + } + final boolean isSystemUser = (flags & UserInfo.FLAG_SYSTEM) != 0; + final boolean isImplicitWhitelistMode = isImplicitWhitelistMode(mode); + final Set<String> whitelistedPackages = getWhitelistedPackagesForUserType(flags); + + final Set<String> installPackages = new ArraySet<>(); + final PackageManagerInternal pmInt = LocalServices.getService(PackageManagerInternal.class); + pmInt.forEachPackage(pkg -> { + if (!pkg.isSystem()) { + return; + } + if (shouldInstallPackage(pkg, mWhitelitsedPackagesForUserTypes, + whitelistedPackages, isImplicitWhitelistMode, isSystemUser)) { + // Although the whitelist uses manifest names, this function returns packageNames. + installPackages.add(pkg.packageName); + } + }); + return installPackages; + } + + /** + * Returns whether the given system package should be installed on the given user, based on the + * the given whitelist of system packages. + * + * @param sysPkg the system package. Must be a system package; no verification for this is done. + * @param userTypeWhitelist map of package manifest names to user flags on which they should be + * installed + * @param userWhitelist set of package manifest names that should be installed on this + * particular user. This must be consistent with userTypeWhitelist, but is + * passed in separately to avoid repeatedly calculating it from + * userTypeWhitelist. + * @param isImplicitWhitelistMode whether non-mentioned packages are implicitly whitelisted. + * @param isSystemUser whether the user is USER_SYSTEM (which gets special treatment). + */ + @VisibleForTesting + static boolean shouldInstallPackage(PackageParser.Package sysPkg, + @NonNull ArrayMap<String, Integer> userTypeWhitelist, + @NonNull Set<String> userWhitelist, boolean isImplicitWhitelistMode, + boolean isSystemUser) { + + final String pkgName = sysPkg.manifestPackageName; + boolean install = (isImplicitWhitelistMode && !userTypeWhitelist.containsKey(pkgName)) + || userWhitelist.contains(pkgName); + + // For the purposes of local development, any package that isn't even mentioned in the + // whitelist at all is implicitly treated as whitelisted for the SYSTEM user. + if (!install && isSystemUser && !userTypeWhitelist.containsKey(pkgName)) { + install = true; + Slog.e(TAG, "System package " + pkgName + " is not mentioned " + + "in SystemConfig's 'install-in-user-type' but we are " + + "implicitly treating it as whitelisted for the SYSTEM user."); + } + return install; + } + + /** + * Gets the package manifest names that are whitelisted for a user with the given flags, + * as determined by SystemConfig. + */ + @VisibleForTesting + @NonNull Set<String> getWhitelistedPackagesForUserType(int flags) { + Set<String> installablePkgs = new ArraySet<>(mWhitelitsedPackagesForUserTypes.size()); + for (int i = 0; i < mWhitelitsedPackagesForUserTypes.size(); i++) { + String pkgName = mWhitelitsedPackagesForUserTypes.keyAt(i); + int whitelistedUserTypes = mWhitelitsedPackagesForUserTypes.valueAt(i); + if ((flags & whitelistedUserTypes) != 0) { + installablePkgs.add(pkgName); + } + } + return installablePkgs; + } + + /** + * Set of package manifest names that are included anywhere in the package-to-user-type + * whitelist, as determined by SystemConfig. + * + * Packages that are whitelisted, but then blacklisted so that they aren't to be installed on + * any user, are still present in this list, since that is a valid scenario (e.g. if an OEM + * completely blacklists an AOSP app). + */ + private Set<String> getWhitelistedSystemPackages() { + return mWhitelitsedPackagesForUserTypes.keySet(); + } + + /** + * Returns a map of package manifest names to the user flags on which it is to be installed. + * Also, clears this data from SystemConfig where it was stored inefficiently (and therefore + * should be called exactly once, even if the data isn't useful). + * + * Any system packages not present in this map should not even be on the device at all. + * To enforce this: + * <ul> + * <li>Illegal user types are ignored.</li> + * <li>Packages that never whitelisted at all (even if they are explicitly blacklisted) are + * ignored.</li> + * <li>Packages that are blacklisted whenever they are whitelisted will be stored with the + * flag 0 (since this is a valid scenario, e.g. if an OEM completely blacklists an AOSP + * app).</li> + * </ul> + */ + @VisibleForTesting + static ArrayMap<String, Integer> determineWhitelistedPackagesForUserTypes( + SystemConfig sysConfig) { + + final ArrayMap<String, Set<String>> whitelist = + sysConfig.getAndClearPackageToUserTypeWhitelist(); + // result maps packageName -> userTypes on which the package should be installed. + final ArrayMap<String, Integer> result = new ArrayMap<>(whitelist.size() + 1); + // First, do the whitelisted user types. + for (int i = 0; i < whitelist.size(); i++) { + final String pkgName = whitelist.keyAt(i); + final int flags = getFlagsFromUserTypes(whitelist.valueAt(i)); + if (flags != 0) { + result.put(pkgName, flags); + } + } + // Then, un-whitelist any blacklisted user types. + // TODO(b/141370854): Right now, the blacklist is actually just an 'unwhitelist'. Which + // direction we go depends on how we design user subtypes, which is still + // being designed. For now, unwhitelisting works for current use-cases. + final ArrayMap<String, Set<String>> blacklist = + sysConfig.getAndClearPackageToUserTypeBlacklist(); + for (int i = 0; i < blacklist.size(); i++) { + final String pkgName = blacklist.keyAt(i); + final int nonFlags = getFlagsFromUserTypes(blacklist.valueAt(i)); + final Integer flags = result.get(pkgName); + if (flags != null) { + result.put(pkgName, flags & ~nonFlags); + } + } + // Regardless of the whitelists/blacklists, ensure mandatory packages. + result.put("android", + UserInfo.FLAG_SYSTEM | UserInfo.FLAG_FULL | UserInfo.PROFILE_FLAGS_MASK); + return result; + } + + /** Converts a user types, as used in SystemConfig, to a UserInfo flag. */ + private static int getFlagsFromUserTypes(Iterable<String> userTypes) { + int flags = 0; + for (String type : userTypes) { + switch (type) { + case "GUEST": + flags |= UserInfo.FLAG_GUEST; + break; + case "RESTRICTED": + flags |= UserInfo.FLAG_RESTRICTED; + break; + case "MANAGED_PROFILE": + flags |= UserInfo.FLAG_MANAGED_PROFILE; + break; + case "EPHEMERAL": + flags |= UserInfo.FLAG_EPHEMERAL; + break; + case "DEMO": + flags |= UserInfo.FLAG_DEMO; + break; + case "FULL": + flags |= UserInfo.FLAG_FULL; + break; + case "SYSTEM": + flags |= UserInfo.FLAG_SYSTEM; + break; + case "PROFILE": + flags |= UserInfo.PROFILE_FLAGS_MASK; + break; + default: + Slog.w(TAG, "SystemConfig contained an invalid user type: " + type); + break; + // Other UserInfo flags are forbidden. + // In particular, FLAG_INITIALIZED, FLAG_DISABLED, FLAG_QUIET_MODE are inapplicable. + // The following are invalid now, but are reconsiderable: FLAG_PRIMARY, FLAG_ADMIN. + } + } + return flags; + } + + void dump(PrintWriter pw) { + for (int i = 0; i < mWhitelitsedPackagesForUserTypes.size(); i++) { + final String pkgName = mWhitelitsedPackagesForUserTypes.keyAt(i); + final String whitelistedUserTypes = + UserInfo.flagsToString(mWhitelitsedPackagesForUserTypes.valueAt(i)); + pw.println(" " + pkgName + ": " + whitelistedUserTypes); + } + } +} diff --git a/services/core/java/com/android/server/protolog/ProtoLogImpl.java b/services/core/java/com/android/server/protolog/ProtoLogImpl.java index 239a4259438b..20bab55f39b1 100644 --- a/services/core/java/com/android/server/protolog/ProtoLogImpl.java +++ b/services/core/java/com/android/server/protolog/ProtoLogImpl.java @@ -110,6 +110,12 @@ public class ProtoLogImpl { getSingleInstance().log(LogLevel.WTF, group, messageHash, paramsMask, messageString, args); } + /** Returns true iff logging is enabled for the given {@code IProtoLogGroup}. */ + public static boolean isEnabled(IProtoLogGroup group) { + return group.isLogToProto() + || (group.isLogToProto() && getSingleInstance().isProtoEnabled()); + } + private static final int BUFFER_CAPACITY = 1024 * 1024; private static final String LOG_FILENAME = "/data/misc/wmtrace/wm_log.pb"; private static final String VIEWER_CONFIG_FILENAME = "/system/etc/protolog.conf.json.gz"; @@ -222,48 +228,50 @@ public class ProtoLogImpl { os.write(MESSAGE_HASH, messageHash); os.write(ELAPSED_REALTIME_NANOS, SystemClock.elapsedRealtimeNanos()); - int argIndex = 0; - ArrayList<Long> longParams = new ArrayList<>(); - ArrayList<Double> doubleParams = new ArrayList<>(); - ArrayList<Boolean> booleanParams = new ArrayList<>(); - for (Object o : args) { - int type = LogDataType.bitmaskToLogDataType(paramsMask, argIndex); - try { - switch (type) { - case LogDataType.STRING: - os.write(STR_PARAMS, o.toString()); - break; - case LogDataType.LONG: - longParams.add(((Number) o).longValue()); - break; - case LogDataType.DOUBLE: - doubleParams.add(((Number) o).doubleValue()); - break; - case LogDataType.BOOLEAN: - booleanParams.add((boolean) o); - break; + if (args != null) { + int argIndex = 0; + ArrayList<Long> longParams = new ArrayList<>(); + ArrayList<Double> doubleParams = new ArrayList<>(); + ArrayList<Boolean> booleanParams = new ArrayList<>(); + for (Object o : args) { + int type = LogDataType.bitmaskToLogDataType(paramsMask, argIndex); + try { + switch (type) { + case LogDataType.STRING: + os.write(STR_PARAMS, o.toString()); + break; + case LogDataType.LONG: + longParams.add(((Number) o).longValue()); + break; + case LogDataType.DOUBLE: + doubleParams.add(((Number) o).doubleValue()); + break; + case LogDataType.BOOLEAN: + booleanParams.add((boolean) o); + break; + } + } catch (ClassCastException ex) { + // Should not happen unless there is an error in the ProtoLogTool. + os.write(STR_PARAMS, "(INVALID PARAMS_MASK) " + o.toString()); + Slog.e(TAG, "Invalid ProtoLog paramsMask", ex); } - } catch (ClassCastException ex) { - // Should not happen unless there is an error in the ProtoLogTool. - os.write(STR_PARAMS, "(INVALID PARAMS_MASK) " + o.toString()); - Slog.e(TAG, "Invalid ProtoLog paramsMask", ex); + argIndex++; } - argIndex++; - } - if (longParams.size() > 0) { - os.writePackedSInt64(SINT64_PARAMS, - longParams.stream().mapToLong(i -> i).toArray()); - } - if (doubleParams.size() > 0) { - os.writePackedDouble(DOUBLE_PARAMS, - doubleParams.stream().mapToDouble(i -> i).toArray()); - } - if (booleanParams.size() > 0) { - boolean[] arr = new boolean[booleanParams.size()]; - for (int i = 0; i < booleanParams.size(); i++) { - arr[i] = booleanParams.get(i); + if (longParams.size() > 0) { + os.writePackedSInt64(SINT64_PARAMS, + longParams.stream().mapToLong(i -> i).toArray()); + } + if (doubleParams.size() > 0) { + os.writePackedDouble(DOUBLE_PARAMS, + doubleParams.stream().mapToDouble(i -> i).toArray()); + } + if (booleanParams.size() > 0) { + boolean[] arr = new boolean[booleanParams.size()]; + for (int i = 0; i < booleanParams.size(); i++) { + arr[i] = booleanParams.get(i); + } + os.writePackedBool(BOOLEAN_PARAMS, arr); } - os.writePackedBool(BOOLEAN_PARAMS, arr); } os.end(token); mBuffer.add(os); diff --git a/services/core/java/com/android/server/stats/ProcfsMemoryUtil.java b/services/core/java/com/android/server/stats/ProcfsMemoryUtil.java index d49b9589cb15..2b25b8921540 100644 --- a/services/core/java/com/android/server/stats/ProcfsMemoryUtil.java +++ b/services/core/java/com/android/server/stats/ProcfsMemoryUtil.java @@ -15,6 +15,7 @@ */ package com.android.server.stats; +import android.annotation.Nullable; import android.os.FileUtils; import android.util.Slog; @@ -22,61 +23,53 @@ import com.android.internal.annotations.VisibleForTesting; import java.io.File; import java.io.IOException; -import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; final class ProcfsMemoryUtil { private static final String TAG = "ProcfsMemoryUtil"; - /** Path to procfs status file: /proc/pid/status. */ - private static final String STATUS_FILE_FMT = "/proc/%d/status"; - - private static final Pattern RSS_HIGH_WATER_MARK_IN_KILOBYTES = - Pattern.compile("VmHWM:\\s*(\\d+)\\s*kB"); - private static final Pattern RSS_IN_KILOBYTES = - Pattern.compile("VmRSS:\\s*(\\d+)\\s*kB"); - private static final Pattern ANON_RSS_IN_KILOBYTES = - Pattern.compile("RssAnon:\\s*(\\d+)\\s*kB"); - private static final Pattern SWAP_IN_KILOBYTES = - Pattern.compile("VmSwap:\\s*(\\d+)\\s*kB"); + private static final Pattern STATUS_MEMORY_STATS = + Pattern.compile(String.join( + ".*", + "Uid:\\s*(\\d+)\\s*", + "VmHWM:\\s*(\\d+)\\s*kB", + "VmRSS:\\s*(\\d+)\\s*kB", + "RssAnon:\\s*(\\d+)\\s*kB", + "VmSwap:\\s*(\\d+)\\s*kB"), Pattern.DOTALL); private ProcfsMemoryUtil() {} /** - * Reads RSS high-water mark of a process from procfs. Returns value of the VmHWM field in - * /proc/PID/status in kilobytes or 0 if not available. - */ - static int readRssHighWaterMarkFromProcfs(int pid) { - final String statusPath = String.format(Locale.US, STATUS_FILE_FMT, pid); - return parseVmHWMFromStatus(readFile(statusPath)); - } - - /** - * Parses RSS high-water mark out from the contents of the /proc/pid/status file in procfs. The - * returned value is in kilobytes. - */ - @VisibleForTesting - static int parseVmHWMFromStatus(String contents) { - return tryParseInt(contents, RSS_HIGH_WATER_MARK_IN_KILOBYTES); - } - - /** - * Reads memory stat of a process from procfs. Returns values of the VmRss, AnonRSS, VmSwap - * fields in /proc/pid/status in kilobytes or 0 if not available. + * Reads memory stats of a process from procfs. Returns values of the VmHWM, VmRss, AnonRSS, + * VmSwap fields in /proc/pid/status in kilobytes or null if not available. */ + @Nullable static MemorySnapshot readMemorySnapshotFromProcfs(int pid) { - final String statusPath = String.format(Locale.US, STATUS_FILE_FMT, pid); - return parseMemorySnapshotFromStatus(readFile(statusPath)); + return parseMemorySnapshotFromStatus(readFile("/proc/" + pid + "/status")); } @VisibleForTesting + @Nullable static MemorySnapshot parseMemorySnapshotFromStatus(String contents) { - final MemorySnapshot snapshot = new MemorySnapshot(); - snapshot.rssInKilobytes = tryParseInt(contents, RSS_IN_KILOBYTES); - snapshot.anonRssInKilobytes = tryParseInt(contents, ANON_RSS_IN_KILOBYTES); - snapshot.swapInKilobytes = tryParseInt(contents, SWAP_IN_KILOBYTES); - return snapshot; + if (contents.isEmpty()) { + return null; + } + try { + final Matcher matcher = STATUS_MEMORY_STATS.matcher(contents); + if (matcher.find()) { + final MemorySnapshot snapshot = new MemorySnapshot(); + snapshot.uid = Integer.parseInt(matcher.group(1)); + snapshot.rssHighWaterMarkInKilobytes = Integer.parseInt(matcher.group(2)); + snapshot.rssInKilobytes = Integer.parseInt(matcher.group(3)); + snapshot.anonRssInKilobytes = Integer.parseInt(matcher.group(4)); + snapshot.swapInKilobytes = Integer.parseInt(matcher.group(5)); + return snapshot; + } + } catch (NumberFormatException e) { + Slog.e(TAG, "Failed to parse value", e); + } + return null; } private static String readFile(String path) { @@ -88,26 +81,11 @@ final class ProcfsMemoryUtil { } } - private static int tryParseInt(String contents, Pattern pattern) { - if (contents.isEmpty()) { - return 0; - } - final Matcher matcher = pattern.matcher(contents); - try { - return matcher.find() ? Integer.parseInt(matcher.group(1)) : 0; - } catch (NumberFormatException e) { - Slog.e(TAG, "Failed to parse value", e); - return 0; - } - } - static final class MemorySnapshot { + public int uid; + public int rssHighWaterMarkInKilobytes; public int rssInKilobytes; public int anonRssInKilobytes; public int swapInKilobytes; - - boolean isEmpty() { - return (anonRssInKilobytes + swapInKilobytes) == 0; - } } } diff --git a/services/core/java/com/android/server/stats/StatsCompanionService.java b/services/core/java/com/android/server/stats/StatsCompanionService.java index e1a48ed3b550..67830a930caf 100644 --- a/services/core/java/com/android/server/stats/StatsCompanionService.java +++ b/services/core/java/com/android/server/stats/StatsCompanionService.java @@ -30,7 +30,6 @@ import static com.android.server.am.MemoryStatUtil.readMemoryStatFromProcfs; import static com.android.server.stats.IonMemoryUtil.readProcessSystemIonHeapSizesFromDebugfs; import static com.android.server.stats.IonMemoryUtil.readSystemIonHeapSizeFromDebugfs; import static com.android.server.stats.ProcfsMemoryUtil.readMemorySnapshotFromProcfs; -import static com.android.server.stats.ProcfsMemoryUtil.readRssHighWaterMarkFromProcfs; import android.annotation.NonNull; import android.annotation.Nullable; @@ -245,6 +244,13 @@ public class StatsCompanionService extends IStatsCompanionService.Stub { "zygote", "zygote64", }; + /** + * Lowest available uid for apps. + * + * <p>Used to quickly discard memory snapshots of the zygote forks from native process + * measurements. + */ + private static final int MIN_APP_UID = 10_000; private static final int CPU_TIME_PER_THREAD_FREQ_MAX_NUM_FREQUENCIES = 8; @@ -1197,20 +1203,18 @@ public class StatsCompanionService extends IStatsCompanionService.Stub { private void pullNativeProcessMemoryState( int tagId, long elapsedNanos, long wallClockNanos, List<StatsLogEventWrapper> pulledData) { - final List<String> processNames = Arrays.asList(MEMORY_INTERESTING_NATIVE_PROCESSES); int[] pids = getPidsForCommands(MEMORY_INTERESTING_NATIVE_PROCESSES); - for (int i = 0; i < pids.length; i++) { - int pid = pids[i]; + for (int pid : pids) { + String processName = readCmdlineFromProcfs(pid); MemoryStat memoryStat = readMemoryStatFromProcfs(pid); if (memoryStat == null) { continue; } int uid = getUidForPid(pid); - String processName = readCmdlineFromProcfs(pid); - // Sometimes we get here processName that is not included in the whitelist. It comes + // Sometimes we get here a process that is not included in the whitelist. It comes // from forking the zygote for an app. We can ignore that sample because this process // is collected by ProcessMemoryState. - if (!processNames.contains(processName)) { + if (isAppUid(uid)) { continue; } StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); @@ -1238,34 +1242,37 @@ public class StatsCompanionService extends IStatsCompanionService.Stub { LocalServices.getService( ActivityManagerInternal.class).getMemoryStateForProcesses(); for (ProcessMemoryState managedProcess : managedProcessList) { - final int rssHighWaterMarkInKilobytes = - readRssHighWaterMarkFromProcfs(managedProcess.pid); - if (rssHighWaterMarkInKilobytes == 0) { + final MemorySnapshot snapshot = readMemorySnapshotFromProcfs(managedProcess.pid); + if (snapshot == null) { continue; } StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); e.writeInt(managedProcess.uid); e.writeString(managedProcess.processName); // RSS high-water mark in bytes. - e.writeLong((long) rssHighWaterMarkInKilobytes * 1024L); - e.writeInt(rssHighWaterMarkInKilobytes); + e.writeLong((long) snapshot.rssHighWaterMarkInKilobytes * 1024L); + e.writeInt(snapshot.rssHighWaterMarkInKilobytes); pulledData.add(e); } int[] pids = getPidsForCommands(MEMORY_INTERESTING_NATIVE_PROCESSES); - for (int i = 0; i < pids.length; i++) { - final int pid = pids[i]; - final int uid = getUidForPid(pid); + for (int pid : pids) { final String processName = readCmdlineFromProcfs(pid); - final int rssHighWaterMarkInKilobytes = readRssHighWaterMarkFromProcfs(pid); - if (rssHighWaterMarkInKilobytes == 0) { + final MemorySnapshot snapshot = readMemorySnapshotFromProcfs(pid); + if (snapshot == null) { + continue; + } + // Sometimes we get here a process that is not included in the whitelist. It comes + // from forking the zygote for an app. We can ignore that sample because this process + // is collected by ProcessMemoryState. + if (isAppUid(snapshot.uid)) { continue; } StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); - e.writeInt(uid); + e.writeInt(snapshot.uid); e.writeString(processName); // RSS high-water mark in bytes. - e.writeLong((long) rssHighWaterMarkInKilobytes * 1024L); - e.writeInt(rssHighWaterMarkInKilobytes); + e.writeLong((long) snapshot.rssHighWaterMarkInKilobytes * 1024L); + e.writeInt(snapshot.rssHighWaterMarkInKilobytes); pulledData.add(e); } // Invoke rss_hwm_reset binary to reset RSS HWM counters for all processes. @@ -1279,15 +1286,15 @@ public class StatsCompanionService extends IStatsCompanionService.Stub { LocalServices.getService( ActivityManagerInternal.class).getMemoryStateForProcesses(); for (ProcessMemoryState managedProcess : managedProcessList) { + final MemorySnapshot snapshot = readMemorySnapshotFromProcfs(managedProcess.pid); + if (snapshot == null) { + continue; + } StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); e.writeInt(managedProcess.uid); e.writeString(managedProcess.processName); e.writeInt(managedProcess.pid); e.writeInt(managedProcess.oomScore); - final MemorySnapshot snapshot = readMemorySnapshotFromProcfs(managedProcess.pid); - if (snapshot.isEmpty()) { - continue; - } e.writeInt(snapshot.rssInKilobytes); e.writeInt(snapshot.anonRssInKilobytes); e.writeInt(snapshot.swapInKilobytes); @@ -1296,15 +1303,22 @@ public class StatsCompanionService extends IStatsCompanionService.Stub { } int[] pids = getPidsForCommands(MEMORY_INTERESTING_NATIVE_PROCESSES); for (int pid : pids) { - StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); - e.writeInt(getUidForPid(pid)); - e.writeString(readCmdlineFromProcfs(pid)); - e.writeInt(pid); - e.writeInt(-1001); // Placeholder for native processes, OOM_SCORE_ADJ_MIN - 1. + final String processName = readCmdlineFromProcfs(pid); final MemorySnapshot snapshot = readMemorySnapshotFromProcfs(pid); - if (snapshot.isEmpty()) { + if (snapshot == null) { continue; } + // Sometimes we get here a process that is not included in the whitelist. It comes + // from forking the zygote for an app. We can ignore that sample because this process + // is collected by ProcessMemoryState. + if (isAppUid(snapshot.uid)) { + continue; + } + StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, elapsedNanos, wallClockNanos); + e.writeInt(snapshot.uid); + e.writeString(processName); + e.writeInt(pid); + e.writeInt(-1001); // Placeholder for native processes, OOM_SCORE_ADJ_MIN - 1. e.writeInt(snapshot.rssInKilobytes); e.writeInt(snapshot.anonRssInKilobytes); e.writeInt(snapshot.swapInKilobytes); @@ -1313,6 +1327,10 @@ public class StatsCompanionService extends IStatsCompanionService.Stub { } } + private static boolean isAppUid(int uid) { + return uid >= MIN_APP_UID; + } + private void pullSystemIonHeapSize( int tagId, long elapsedNanos, long wallClockNanos, List<StatsLogEventWrapper> pulledData) { diff --git a/services/core/java/com/android/server/tv/TvRemoteProviderProxy.java b/services/core/java/com/android/server/tv/TvRemoteProviderProxy.java index f8ffb7c1c0e2..b7bc77dc97ee 100644 --- a/services/core/java/com/android/server/tv/TvRemoteProviderProxy.java +++ b/services/core/java/com/android/server/tv/TvRemoteProviderProxy.java @@ -21,8 +21,6 @@ import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.media.tv.ITvRemoteProvider; -import android.media.tv.ITvRemoteServiceInput; -import android.os.Binder; import android.os.IBinder; import android.os.RemoteException; import android.os.UserHandle; @@ -30,44 +28,33 @@ import android.util.Log; import android.util.Slog; import java.io.PrintWriter; -import java.lang.ref.WeakReference; /** * Maintains a connection to a tv remote provider service. */ final class TvRemoteProviderProxy implements ServiceConnection { - private static final String TAG = "TvRemoteProvProxy"; // max. 23 chars + private static final String TAG = "TvRemoteProviderProxy"; private static final boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE); - private static final boolean DEBUG_KEY = false; // This should match TvRemoteProvider.ACTION_TV_REMOTE_PROVIDER protected static final String SERVICE_INTERFACE = "com.android.media.tv.remoteprovider.TvRemoteProvider"; private final Context mContext; + private final Object mLock; private final ComponentName mComponentName; private final int mUserId; private final int mUid; - /** - * State guarded by mLock. - * This is the first lock in sequence for an incoming call. - * The second lock is always {@link TvRemoteService#mLock} - * - * There are currently no methods that break this sequence. - */ - private final Object mLock = new Object(); - - private ProviderMethods mProviderMethods; - // Connection state + // State changes happen only in the main thread, hence no lock is needed private boolean mRunning; private boolean mBound; - private Connection mActiveConnection; + private boolean mConnected; - TvRemoteProviderProxy(Context context, ProviderMethods provider, + TvRemoteProviderProxy(Context context, Object lock, ComponentName componentName, int userId, int uid) { mContext = context; - mProviderMethods = provider; + mLock = lock; mComponentName = componentName; mUserId = userId; mUid = uid; @@ -78,7 +65,7 @@ final class TvRemoteProviderProxy implements ServiceConnection { pw.println(prefix + " mUserId=" + mUserId); pw.println(prefix + " mRunning=" + mRunning); pw.println(prefix + " mBound=" + mBound); - pw.println(prefix + " mActiveConnection=" + mActiveConnection); + pw.println(prefix + " mConnected=" + mConnected); } public boolean hasComponentName(String packageName, String className) { @@ -109,11 +96,9 @@ final class TvRemoteProviderProxy implements ServiceConnection { } public void rebindIfDisconnected() { - synchronized (mLock) { - if (mActiveConnection == null && mRunning) { - unbind(); - bind(); - } + if (mRunning && !mConnected) { + unbind(); + bind(); } } @@ -129,7 +114,7 @@ final class TvRemoteProviderProxy implements ServiceConnection { mBound = mContext.bindServiceAsUser(service, this, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, new UserHandle(mUserId)); - if (!mBound && DEBUG) { + if (DEBUG && !mBound) { Slog.d(TAG, this + ": Bind failed"); } } catch (SecurityException ex) { @@ -147,7 +132,6 @@ final class TvRemoteProviderProxy implements ServiceConnection { } mBound = false; - disconnect(); mContext.unbindService(this); } } @@ -158,392 +142,27 @@ final class TvRemoteProviderProxy implements ServiceConnection { Slog.d(TAG, this + ": onServiceConnected()"); } - if (mBound) { - disconnect(); + mConnected = true; - ITvRemoteProvider provider = ITvRemoteProvider.Stub.asInterface(service); - if (provider != null) { - Connection connection = new Connection(provider); - if (connection.register()) { - synchronized (mLock) { - mActiveConnection = connection; - } - if (DEBUG) { - Slog.d(TAG, this + ": Connected successfully."); - } - } else { - if (DEBUG) { - Slog.d(TAG, this + ": Registration failed"); - } - } - } else { - Slog.e(TAG, this + ": Service returned invalid remote-control provider binder"); - } + final ITvRemoteProvider provider = ITvRemoteProvider.Stub.asInterface(service); + if (provider == null) { + Slog.e(TAG, this + ": Invalid binder"); + return; } - } - @Override - public void onServiceDisconnected(ComponentName name) { - if (DEBUG) Slog.d(TAG, this + ": Service disconnected"); - disconnect(); - } - - private void disconnect() { - synchronized (mLock) { - if (mActiveConnection != null) { - mActiveConnection.dispose(); - mActiveConnection = null; - } + try { + provider.setRemoteServiceInputSink(new TvRemoteServiceInput(mLock, provider)); + } catch (RemoteException e) { + Slog.e(TAG, this + ": Failed remote call to setRemoteServiceInputSink"); } } - interface ProviderMethods { - // InputBridge - boolean openInputBridge(TvRemoteProviderProxy provider, IBinder token, String name, - int width, int height, int maxPointers); - - void closeInputBridge(TvRemoteProviderProxy provider, IBinder token); - - void clearInputBridge(TvRemoteProviderProxy provider, IBinder token); - - void sendKeyDown(TvRemoteProviderProxy provider, IBinder token, int keyCode); - - void sendKeyUp(TvRemoteProviderProxy provider, IBinder token, int keyCode); - - void sendPointerDown(TvRemoteProviderProxy provider, IBinder token, int pointerId, int x, - int y); - - void sendPointerUp(TvRemoteProviderProxy provider, IBinder token, int pointerId); - - void sendPointerSync(TvRemoteProviderProxy provider, IBinder token); - } - - private final class Connection { - private final ITvRemoteProvider mTvRemoteProvider; - private final RemoteServiceInputProvider mServiceInputProvider; - - public Connection(ITvRemoteProvider provider) { - mTvRemoteProvider = provider; - mServiceInputProvider = new RemoteServiceInputProvider(this); - } - - public boolean register() { - if (DEBUG) Slog.d(TAG, "Connection::register()"); - try { - mTvRemoteProvider.setRemoteServiceInputSink(mServiceInputProvider); - return true; - } catch (RemoteException ex) { - dispose(); - return false; - } - } - - public void dispose() { - if (DEBUG) Slog.d(TAG, "Connection::dispose()"); - mServiceInputProvider.dispose(); - } - - - public void onInputBridgeConnected(IBinder token) { - if (DEBUG) Slog.d(TAG, this + ": onInputBridgeConnected"); - try { - mTvRemoteProvider.onInputBridgeConnected(token); - } catch (RemoteException ex) { - Slog.e(TAG, "Failed to deliver onInputBridgeConnected. ", ex); - } - } - - void openInputBridge(final IBinder token, final String name, final int width, - final int height, final int maxPointers) { - synchronized (mLock) { - if (mActiveConnection == this && Binder.getCallingUid() == mUid) { - if (DEBUG) { - Slog.d(TAG, this + ": openInputBridge," + - " token=" + token + ", name=" + name); - } - final long idToken = Binder.clearCallingIdentity(); - try { - if (mProviderMethods.openInputBridge(TvRemoteProviderProxy.this, token, - name, width, height, maxPointers)) { - onInputBridgeConnected(token); - } - } finally { - Binder.restoreCallingIdentity(idToken); - } - } else { - if (DEBUG) { - Slog.w(TAG, - "openInputBridge, Invalid connection or incorrect uid: " + Binder - .getCallingUid()); - } - } - } - } - - void closeInputBridge(final IBinder token) { - synchronized (mLock) { - if (mActiveConnection == this && Binder.getCallingUid() == mUid) { - if (DEBUG) { - Slog.d(TAG, this + ": closeInputBridge," + - " token=" + token); - } - final long idToken = Binder.clearCallingIdentity(); - try { - mProviderMethods.closeInputBridge(TvRemoteProviderProxy.this, token); - } finally { - Binder.restoreCallingIdentity(idToken); - } - } else { - if (DEBUG) { - Slog.w(TAG, - "closeInputBridge, Invalid connection or incorrect uid: " + - Binder.getCallingUid()); - } - } - } - } - - void clearInputBridge(final IBinder token) { - synchronized (mLock) { - if (mActiveConnection == this && Binder.getCallingUid() == mUid) { - if (DEBUG) { - Slog.d(TAG, this + ": clearInputBridge," + - " token=" + token); - } - final long idToken = Binder.clearCallingIdentity(); - try { - mProviderMethods.clearInputBridge(TvRemoteProviderProxy.this, token); - } finally { - Binder.restoreCallingIdentity(idToken); - } - } else { - if (DEBUG) { - Slog.w(TAG, - "clearInputBridge, Invalid connection or incorrect uid: " + - Binder.getCallingUid()); - } - } - } - } - - void sendTimestamp(final IBinder token, final long timestamp) { - if (DEBUG) { - Slog.e(TAG, "sendTimestamp is deprecated, please remove all usages of this API."); - } - } - - void sendKeyDown(final IBinder token, final int keyCode) { - synchronized (mLock) { - if (mActiveConnection == this && Binder.getCallingUid() == mUid) { - if (DEBUG_KEY) { - Slog.d(TAG, this + ": sendKeyDown," + - " token=" + token + ", keyCode=" + keyCode); - } - final long idToken = Binder.clearCallingIdentity(); - try { - mProviderMethods.sendKeyDown(TvRemoteProviderProxy.this, token, keyCode); - } finally { - Binder.restoreCallingIdentity(idToken); - } - } else { - if (DEBUG) { - Slog.w(TAG, - "sendKeyDown, Invalid connection or incorrect uid: " + Binder - .getCallingUid()); - } - } - } - } - - void sendKeyUp(final IBinder token, final int keyCode) { - synchronized (mLock) { - if (mActiveConnection == this && Binder.getCallingUid() == mUid) { - if (DEBUG_KEY) { - Slog.d(TAG, this + ": sendKeyUp," + - " token=" + token + ", keyCode=" + keyCode); - } - final long idToken = Binder.clearCallingIdentity(); - try { - mProviderMethods.sendKeyUp(TvRemoteProviderProxy.this, token, keyCode); - } finally { - Binder.restoreCallingIdentity(idToken); - } - } else { - if (DEBUG) { - Slog.w(TAG, - "sendKeyUp, Invalid connection or incorrect uid: " + Binder - .getCallingUid()); - } - } - } - } - - void sendPointerDown(final IBinder token, final int pointerId, final int x, final int y) { - synchronized (mLock) { - if (mActiveConnection == this && Binder.getCallingUid() == mUid) { - if (DEBUG_KEY) { - Slog.d(TAG, this + ": sendPointerDown," + - " token=" + token + ", pointerId=" + pointerId); - } - final long idToken = Binder.clearCallingIdentity(); - try { - mProviderMethods.sendPointerDown(TvRemoteProviderProxy.this, token, - pointerId, x, y); - } finally { - Binder.restoreCallingIdentity(idToken); - } - } else { - if (DEBUG) { - Slog.w(TAG, - "sendPointerDown, Invalid connection or incorrect uid: " + Binder - .getCallingUid()); - } - } - } - } - - void sendPointerUp(final IBinder token, final int pointerId) { - synchronized (mLock) { - if (mActiveConnection == this && Binder.getCallingUid() == mUid) { - if (DEBUG_KEY) { - Slog.d(TAG, this + ": sendPointerUp," + - " token=" + token + ", pointerId=" + pointerId); - } - final long idToken = Binder.clearCallingIdentity(); - try { - mProviderMethods.sendPointerUp(TvRemoteProviderProxy.this, token, - pointerId); - } finally { - Binder.restoreCallingIdentity(idToken); - } - } else { - if (DEBUG) { - Slog.w(TAG, - "sendPointerUp, Invalid connection or incorrect uid: " + Binder - .getCallingUid()); - } - } - } - } - - void sendPointerSync(final IBinder token) { - synchronized (mLock) { - if (mActiveConnection == this && Binder.getCallingUid() == mUid) { - if (DEBUG_KEY) { - Slog.d(TAG, this + ": sendPointerSync," + - " token=" + token); - } - final long idToken = Binder.clearCallingIdentity(); - try { - if (mProviderMethods != null) { - mProviderMethods.sendPointerSync(TvRemoteProviderProxy.this, token); - } - } finally { - Binder.restoreCallingIdentity(idToken); - } - } else { - if (DEBUG) { - Slog.w(TAG, - "sendPointerSync, Invalid connection or incorrect uid: " + Binder - .getCallingUid()); - } - } - } - } - } - - /** - * Receives events from the connected provider. - * <p> - * This inner class is static and only retains a weak reference to the connection - * to prevent the client from being leaked in case the service is holding an - * active reference to the client's callback. - * </p> - */ - private static final class RemoteServiceInputProvider extends ITvRemoteServiceInput.Stub { - private final WeakReference<Connection> mConnectionRef; - - public RemoteServiceInputProvider(Connection connection) { - mConnectionRef = new WeakReference<Connection>(connection); - } - - public void dispose() { - // Terminate the connection. - mConnectionRef.clear(); - } - - @Override - public void openInputBridge(IBinder token, String name, int width, - int height, int maxPointers) throws RemoteException { - Connection connection = mConnectionRef.get(); - if (connection != null) { - connection.openInputBridge(token, name, width, height, maxPointers); - } - } - - @Override - public void closeInputBridge(IBinder token) throws RemoteException { - Connection connection = mConnectionRef.get(); - if (connection != null) { - connection.closeInputBridge(token); - } - } - - @Override - public void clearInputBridge(IBinder token) throws RemoteException { - Connection connection = mConnectionRef.get(); - if (connection != null) { - connection.clearInputBridge(token); - } - } - - @Override - public void sendTimestamp(IBinder token, long timestamp) throws RemoteException { - Connection connection = mConnectionRef.get(); - if (connection != null) { - connection.sendTimestamp(token, timestamp); - } - } - - @Override - public void sendKeyDown(IBinder token, int keyCode) throws RemoteException { - Connection connection = mConnectionRef.get(); - if (connection != null) { - connection.sendKeyDown(token, keyCode); - } - } - - @Override - public void sendKeyUp(IBinder token, int keyCode) throws RemoteException { - Connection connection = mConnectionRef.get(); - if (connection != null) { - connection.sendKeyUp(token, keyCode); - } - } - - @Override - public void sendPointerDown(IBinder token, int pointerId, int x, int y) - throws RemoteException { - Connection connection = mConnectionRef.get(); - if (connection != null) { - connection.sendPointerDown(token, pointerId, x, y); - } - } - - @Override - public void sendPointerUp(IBinder token, int pointerId) throws RemoteException { - Connection connection = mConnectionRef.get(); - if (connection != null) { - connection.sendPointerUp(token, pointerId); - } - } + @Override + public void onServiceDisconnected(ComponentName name) { + mConnected = false; - @Override - public void sendPointerSync(IBinder token) throws RemoteException { - Connection connection = mConnectionRef.get(); - if (connection != null) { - connection.sendPointerSync(token); - } + if (DEBUG) { + Slog.d(TAG, this + ": onServiceDisconnected()"); } } } diff --git a/services/core/java/com/android/server/tv/TvRemoteProviderWatcher.java b/services/core/java/com/android/server/tv/TvRemoteProviderWatcher.java index 0d29edd02663..cddcabe80f33 100644 --- a/services/core/java/com/android/server/tv/TvRemoteProviderWatcher.java +++ b/services/core/java/com/android/server/tv/TvRemoteProviderWatcher.java @@ -41,27 +41,27 @@ import java.util.Collections; */ final class TvRemoteProviderWatcher { - private static final String TAG = "TvRemoteProvWatcher"; // max. 23 chars + private static final String TAG = "TvRemoteProviderWatcher"; private static final boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE); private final Context mContext; - private final TvRemoteProviderProxy.ProviderMethods mProvider; private final Handler mHandler; private final PackageManager mPackageManager; private final ArrayList<TvRemoteProviderProxy> mProviderProxies = new ArrayList<>(); private final int mUserId; private final String mUnbundledServicePackage; + private final Object mLock; private boolean mRunning; - TvRemoteProviderWatcher(Context context, TvRemoteProviderProxy.ProviderMethods provider) { + TvRemoteProviderWatcher(Context context, Object lock) { mContext = context; - mProvider = provider; mHandler = new Handler(true); mUserId = UserHandle.myUserId(); mPackageManager = context.getPackageManager(); mUnbundledServicePackage = context.getString( com.android.internal.R.string.config_tvRemoteServicePackage); + mLock = lock; } public void start() { @@ -116,7 +116,7 @@ final class TvRemoteProviderWatcher { int sourceIndex = findProvider(serviceInfo.packageName, serviceInfo.name); if (sourceIndex < 0) { TvRemoteProviderProxy providerProxy = - new TvRemoteProviderProxy(mContext, mProvider, + new TvRemoteProviderProxy(mContext, mLock, new ComponentName(serviceInfo.packageName, serviceInfo.name), mUserId, serviceInfo.applicationInfo.uid); providerProxy.start(); diff --git a/services/core/java/com/android/server/tv/TvRemoteService.java b/services/core/java/com/android/server/tv/TvRemoteService.java index bee6fb34a899..58946456b940 100644 --- a/services/core/java/com/android/server/tv/TvRemoteService.java +++ b/services/core/java/com/android/server/tv/TvRemoteService.java @@ -17,17 +17,11 @@ package com.android.server.tv; import android.content.Context; -import android.os.IBinder; -import android.os.RemoteException; -import android.util.ArrayMap; import android.util.Slog; import com.android.server.SystemService; import com.android.server.Watchdog; -import java.io.IOException; -import java.util.Map; - /** * TvRemoteService represents a system service that allows a connected * remote control (emote) service to inject white-listed input events @@ -38,27 +32,17 @@ import java.util.Map; public class TvRemoteService extends SystemService implements Watchdog.Monitor { private static final String TAG = "TvRemoteService"; private static final boolean DEBUG = false; - private static final boolean DEBUG_KEYS = false; - - private final TvRemoteProviderWatcher mWatcher; - private Map<IBinder, UinputBridge> mBridgeMap = new ArrayMap(); /** - * State guarded by mLock. - * This is the second lock in sequence for an incoming call. - * The first lock is always {@link TvRemoteProviderProxy#mLock} - * - * There are currently no methods that break this sequence. - * Special note: - * Outgoing call informInputBridgeConnected(), which is called from - * openInputBridgeInternalLocked() uses a handler thereby relinquishing held locks. + * All actions on input bridges are serialized using mLock. + * This is necessary because {@link UInputBridge} is not thread-safe. */ private final Object mLock = new Object(); + private final TvRemoteProviderWatcher mWatcher; public TvRemoteService(Context context) { super(context); - mWatcher = new TvRemoteProviderWatcher(context, - new UserProvider(TvRemoteService.this)); + mWatcher = new TvRemoteProviderWatcher(context, mLock); Watchdog.getInstance().addMonitor(this); } @@ -81,214 +65,4 @@ public class TvRemoteService extends SystemService implements Watchdog.Monitor { mWatcher.start(); // Also schedules the start of all providers. } } - - private boolean openInputBridgeInternalLocked(final IBinder token, - String name, int width, int height, - int maxPointers) { - if (DEBUG) { - Slog.d(TAG, "openInputBridgeInternalLocked(), token: " + token + ", name: " + name + - ", width: " + width + ", height: " + height + ", maxPointers: " + maxPointers); - } - - try { - //Create a new bridge, if one does not exist already - if (mBridgeMap.containsKey(token)) { - if (DEBUG) Slog.d(TAG, "RemoteBridge already exists"); - return true; - } - - UinputBridge inputBridge = new UinputBridge(token, name, width, height, maxPointers); - mBridgeMap.put(token, inputBridge); - - try { - token.linkToDeath(new IBinder.DeathRecipient() { - @Override - public void binderDied() { - synchronized (mLock) { - closeInputBridgeInternalLocked(token); - } - } - }, 0); - } catch (RemoteException e) { - if (DEBUG) Slog.d(TAG, "Token is already dead"); - closeInputBridgeInternalLocked(token); - return false; - } - } catch (IOException ioe) { - Slog.e(TAG, "Cannot create device for " + name); - return false; - } - return true; - } - - private void closeInputBridgeInternalLocked(IBinder token) { - if (DEBUG) { - Slog.d(TAG, "closeInputBridgeInternalLocked(), token: " + token); - } - - // Close an existing RemoteBridge - UinputBridge inputBridge = mBridgeMap.get(token); - if (inputBridge != null) { - inputBridge.close(token); - } - - mBridgeMap.remove(token); - } - - private void clearInputBridgeInternalLocked(IBinder token) { - if (DEBUG) { - Slog.d(TAG, "clearInputBridgeInternalLocked(), token: " + token); - } - - UinputBridge inputBridge = mBridgeMap.get(token); - if (inputBridge != null) { - inputBridge.clear(token); - } - } - - private void sendKeyDownInternalLocked(IBinder token, int keyCode) { - if (DEBUG_KEYS) { - Slog.d(TAG, "sendKeyDownInternalLocked(), token: " + token + ", keyCode: " + keyCode); - } - - UinputBridge inputBridge = mBridgeMap.get(token); - if (inputBridge != null) { - inputBridge.sendKeyDown(token, keyCode); - } - } - - private void sendKeyUpInternalLocked(IBinder token, int keyCode) { - if (DEBUG_KEYS) { - Slog.d(TAG, "sendKeyUpInternalLocked(), token: " + token + ", keyCode: " + keyCode); - } - - UinputBridge inputBridge = mBridgeMap.get(token); - if (inputBridge != null) { - inputBridge.sendKeyUp(token, keyCode); - } - } - - private void sendPointerDownInternalLocked(IBinder token, int pointerId, int x, int y) { - if (DEBUG_KEYS) { - Slog.d(TAG, "sendPointerDownInternalLocked(), token: " + token + ", pointerId: " + - pointerId + ", x: " + x + ", y: " + y); - } - - UinputBridge inputBridge = mBridgeMap.get(token); - if (inputBridge != null) { - inputBridge.sendPointerDown(token, pointerId, x, y); - } - } - - private void sendPointerUpInternalLocked(IBinder token, int pointerId) { - if (DEBUG_KEYS) { - Slog.d(TAG, "sendPointerUpInternalLocked(), token: " + token + ", pointerId: " + - pointerId); - } - - UinputBridge inputBridge = mBridgeMap.get(token); - if (inputBridge != null) { - inputBridge.sendPointerUp(token, pointerId); - } - } - - private void sendPointerSyncInternalLocked(IBinder token) { - if (DEBUG_KEYS) { - Slog.d(TAG, "sendPointerSyncInternalLocked(), token: " + token); - } - - UinputBridge inputBridge = mBridgeMap.get(token); - if (inputBridge != null) { - inputBridge.sendPointerSync(token); - } - } - - private final class UserProvider implements TvRemoteProviderProxy.ProviderMethods { - - private final TvRemoteService mService; - - public UserProvider(TvRemoteService service) { - mService = service; - } - - @Override - public boolean openInputBridge(TvRemoteProviderProxy provider, IBinder token, String name, - int width, int height, int maxPointers) { - if (DEBUG) { - Slog.d(TAG, "openInputBridge(), token: " + token + - ", name: " + name + ", width: " + width + - ", height: " + height + ", maxPointers: " + maxPointers); - } - - synchronized (mLock) { - return mService.openInputBridgeInternalLocked(token, name, width, - height, maxPointers); - } - } - - @Override - public void closeInputBridge(TvRemoteProviderProxy provider, IBinder token) { - if (DEBUG) Slog.d(TAG, "closeInputBridge(), token: " + token); - synchronized (mLock) { - mService.closeInputBridgeInternalLocked(token); - } - } - - @Override - public void clearInputBridge(TvRemoteProviderProxy provider, IBinder token) { - if (DEBUG) Slog.d(TAG, "clearInputBridge(), token: " + token); - synchronized (mLock) { - mService.clearInputBridgeInternalLocked(token); - } - } - - @Override - public void sendKeyDown(TvRemoteProviderProxy provider, IBinder token, int keyCode) { - if (DEBUG_KEYS) { - Slog.d(TAG, "sendKeyDown(), token: " + token + ", keyCode: " + keyCode); - } - synchronized (mLock) { - mService.sendKeyDownInternalLocked(token, keyCode); - } - } - - @Override - public void sendKeyUp(TvRemoteProviderProxy provider, IBinder token, int keyCode) { - if (DEBUG_KEYS) { - Slog.d(TAG, "sendKeyUp(), token: " + token + ", keyCode: " + keyCode); - } - synchronized (mLock) { - mService.sendKeyUpInternalLocked(token, keyCode); - } - } - - @Override - public void sendPointerDown(TvRemoteProviderProxy provider, IBinder token, int pointerId, - int x, int y) { - if (DEBUG_KEYS) { - Slog.d(TAG, "sendPointerDown(), token: " + token + ", pointerId: " + pointerId); - } - synchronized (mLock) { - mService.sendPointerDownInternalLocked(token, pointerId, x, y); - } - } - - @Override - public void sendPointerUp(TvRemoteProviderProxy provider, IBinder token, int pointerId) { - if (DEBUG_KEYS) { - Slog.d(TAG, "sendPointerUp(), token: " + token + ", pointerId: " + pointerId); - } - synchronized (mLock) { - mService.sendPointerUpInternalLocked(token, pointerId); - } - } - - @Override - public void sendPointerSync(TvRemoteProviderProxy provider, IBinder token) { - if (DEBUG_KEYS) Slog.d(TAG, "sendPointerSync(), token: " + token); - synchronized (mLock) { - mService.sendPointerSyncInternalLocked(token); - } - } - } } diff --git a/services/core/java/com/android/server/tv/TvRemoteServiceInput.java b/services/core/java/com/android/server/tv/TvRemoteServiceInput.java new file mode 100644 index 000000000000..8fe6da5e8dbe --- /dev/null +++ b/services/core/java/com/android/server/tv/TvRemoteServiceInput.java @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2019 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.server.tv; + +import android.media.tv.ITvRemoteProvider; +import android.media.tv.ITvRemoteServiceInput; +import android.os.Binder; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.ArrayMap; +import android.util.Slog; + +import java.io.IOException; +import java.util.Map; + +final class TvRemoteServiceInput extends ITvRemoteServiceInput.Stub { + private static final String TAG = "TvRemoteServiceInput"; + private static final boolean DEBUG = false; + private static final boolean DEBUG_KEYS = false; + + private final Map<IBinder, UinputBridge> mBridgeMap; + private final Object mLock; + private final ITvRemoteProvider mProvider; + + TvRemoteServiceInput(Object lock, ITvRemoteProvider provider) { + mBridgeMap = new ArrayMap(); + mLock = lock; + mProvider = provider; + } + + @Override + public void openInputBridge(IBinder token, String name, int width, + int height, int maxPointers) { + if (DEBUG) { + Slog.d(TAG, "openInputBridge(), token: " + token + + ", name: " + name + ", width: " + width + + ", height: " + height + ", maxPointers: " + maxPointers); + } + + synchronized (mLock) { + if (mBridgeMap.containsKey(token)) { + if (DEBUG) { + Slog.d(TAG, "InputBridge already exists"); + } + } else { + final long idToken = Binder.clearCallingIdentity(); + try { + mBridgeMap.put(token, + new UinputBridge(token, name, width, height, maxPointers)); + token.linkToDeath(new IBinder.DeathRecipient() { + @Override + public void binderDied() { + closeInputBridge(token); + } + }, 0); + } catch (IOException e) { + Slog.e(TAG, "Cannot create device for " + name); + return; + } catch (RemoteException e) { + Slog.e(TAG, "Token is already dead"); + closeInputBridge(token); + return; + } finally { + Binder.restoreCallingIdentity(idToken); + } + } + } + + try { + mProvider.onInputBridgeConnected(token); + } catch (RemoteException e) { + Slog.e(TAG, "Failed remote call to onInputBridgeConnected"); + } + } + + @Override + public void closeInputBridge(IBinder token) { + if (DEBUG) { + Slog.d(TAG, "closeInputBridge(), token: " + token); + } + + synchronized (mLock) { + UinputBridge inputBridge = mBridgeMap.remove(token); + if (inputBridge == null) { + return; + } + + final long idToken = Binder.clearCallingIdentity(); + try { + inputBridge.close(token); + } finally { + Binder.restoreCallingIdentity(idToken); + } + } + } + + @Override + public void clearInputBridge(IBinder token) { + if (DEBUG) { + Slog.d(TAG, "clearInputBridge, token: " + token); + } + + synchronized (mLock) { + UinputBridge inputBridge = mBridgeMap.get(token); + if (inputBridge == null) { + return; + } + + final long idToken = Binder.clearCallingIdentity(); + try { + inputBridge.clear(token); + } finally { + Binder.restoreCallingIdentity(idToken); + } + } + } + + @Override + public void sendTimestamp(IBinder token, long timestamp) { + if (DEBUG) { + Slog.e(TAG, "sendTimestamp is deprecated, please remove all usages of this API."); + } + } + + @Override + public void sendKeyDown(IBinder token, int keyCode) { + if (DEBUG_KEYS) { + Slog.d(TAG, "sendKeyDown(), token: " + token + ", keyCode: " + keyCode); + } + + synchronized (mLock) { + UinputBridge inputBridge = mBridgeMap.get(token); + if (inputBridge == null) { + return; + } + + final long idToken = Binder.clearCallingIdentity(); + try { + inputBridge.sendKeyDown(token, keyCode); + } finally { + Binder.restoreCallingIdentity(idToken); + } + } + } + + @Override + public void sendKeyUp(IBinder token, int keyCode) { + if (DEBUG_KEYS) { + Slog.d(TAG, "sendKeyUp(), token: " + token + ", keyCode: " + keyCode); + } + + synchronized (mLock) { + UinputBridge inputBridge = mBridgeMap.get(token); + if (inputBridge == null) { + return; + } + + final long idToken = Binder.clearCallingIdentity(); + try { + inputBridge.sendKeyUp(token, keyCode); + } finally { + Binder.restoreCallingIdentity(idToken); + } + } + } + + @Override + public void sendPointerDown(IBinder token, int pointerId, int x, int y) { + if (DEBUG_KEYS) { + Slog.d(TAG, "sendPointerDown(), token: " + token + ", pointerId: " + + pointerId + ", x: " + x + ", y: " + y); + } + + synchronized (mLock) { + UinputBridge inputBridge = mBridgeMap.get(token); + if (inputBridge == null) { + return; + } + + final long idToken = Binder.clearCallingIdentity(); + try { + inputBridge.sendPointerDown(token, pointerId, x, y); + } finally { + Binder.restoreCallingIdentity(idToken); + } + } + } + + @Override + public void sendPointerUp(IBinder token, int pointerId) { + if (DEBUG_KEYS) { + Slog.d(TAG, "sendPointerUp(), token: " + token + ", pointerId: " + pointerId); + } + + synchronized (mLock) { + UinputBridge inputBridge = mBridgeMap.get(token); + if (inputBridge == null) { + return; + } + + final long idToken = Binder.clearCallingIdentity(); + try { + inputBridge.sendPointerUp(token, pointerId); + } finally { + Binder.restoreCallingIdentity(idToken); + } + } + } + + @Override + public void sendPointerSync(IBinder token) { + if (DEBUG_KEYS) { + Slog.d(TAG, "sendPointerSync(), token: " + token); + } + + synchronized (mLock) { + UinputBridge inputBridge = mBridgeMap.get(token); + if (inputBridge == null) { + return; + } + + final long idToken = Binder.clearCallingIdentity(); + try { + inputBridge.sendPointerSync(token); + } finally { + Binder.restoreCallingIdentity(idToken); + } + } + } +} diff --git a/services/core/java/com/android/server/wm/PinnedStackController.java b/services/core/java/com/android/server/wm/PinnedStackController.java index 8e57fec6ba46..2e6df601cf0b 100644 --- a/services/core/java/com/android/server/wm/PinnedStackController.java +++ b/services/core/java/com/android/server/wm/PinnedStackController.java @@ -90,8 +90,6 @@ class PinnedStackController { private boolean mIsMinimized; private boolean mIsImeShowing; private int mImeHeight; - private boolean mIsShelfShowing; - private int mShelfHeight; // The set of actions and aspect-ratio for the that are currently allowed on the PiP activity private ArrayList<RemoteAction> mActions = new ArrayList<>(); @@ -216,7 +214,6 @@ class PinnedStackController { mPinnedStackListener = listener; notifyDisplayInfoChanged(mDisplayInfo); notifyImeVisibilityChanged(mIsImeShowing, mImeHeight); - notifyShelfVisibilityChanged(mIsShelfShowing, mShelfHeight); // The movement bounds notification needs to be sent before the minimized state, since // SystemUI may use the bounds to retore the minimized position notifyMovementBoundsChanged(false /* fromImeAdjustment */, @@ -278,9 +275,7 @@ class PinnedStackController { mSnapAlgorithm.applySnapFraction(defaultBounds, movementBounds, snapFraction); } else { Gravity.apply(mDefaultStackGravity, size.getWidth(), size.getHeight(), insetBounds, - 0, Math.max(mIsImeShowing ? mImeHeight : 0, - mIsShelfShowing ? mShelfHeight : 0), - defaultBounds); + 0, mIsImeShowing ? mImeHeight : 0, defaultBounds); } return defaultBounds; } @@ -367,21 +362,6 @@ class PinnedStackController { } /** - * Sets the shelf state and height. - */ - void setAdjustedForShelf(boolean adjustedForShelf, int shelfHeight) { - final boolean shelfShowing = adjustedForShelf && shelfHeight > 0; - if (shelfShowing == mIsShelfShowing && shelfHeight == mShelfHeight) { - return; - } - - mIsShelfShowing = shelfShowing; - mShelfHeight = shelfHeight; - notifyShelfVisibilityChanged(shelfShowing, shelfHeight); - notifyMovementBoundsChanged(false /* fromImeAdjustment */, true /* fromShelfAdjustment */); - } - - /** * Sets the current aspect ratio. */ void setAspectRatio(float aspectRatio) { @@ -439,16 +419,6 @@ class PinnedStackController { } } - private void notifyShelfVisibilityChanged(boolean shelfVisible, int shelfHeight) { - if (mPinnedStackListener != null) { - try { - mPinnedStackListener.onShelfVisibilityChanged(shelfVisible, shelfHeight); - } catch (RemoteException e) { - Slog.e(TAG_WM, "Error delivering bounds changed event.", e); - } - } - } - private void notifyAspectRatioChanged(float aspectRatio) { if (mPinnedStackListener == null) return; try { @@ -613,8 +583,6 @@ class PinnedStackController { pw.println(); pw.println(prefix + " mIsImeShowing=" + mIsImeShowing); pw.println(prefix + " mImeHeight=" + mImeHeight); - pw.println(prefix + " mIsShelfShowing=" + mIsShelfShowing); - pw.println(prefix + " mShelfHeight=" + mShelfHeight); pw.println(prefix + " mIsMinimized=" + mIsMinimized); pw.println(prefix + " mAspectRatio=" + mAspectRatio); pw.println(prefix + " mMinAspectRatio=" + mMinAspectRatio); diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java index 8b227a62ebaa..b4309c74b390 100644 --- a/services/core/java/com/android/server/wm/WindowManagerService.java +++ b/services/core/java/com/android/server/wm/WindowManagerService.java @@ -5594,16 +5594,6 @@ public class WindowManagerService extends IWindowManager.Stub } @Override - public void setShelfHeight(boolean visible, int shelfHeight) { - mAtmInternal.enforceCallerIsRecentsOrHasPermission(android.Manifest.permission.STATUS_BAR, - "setShelfHeight()"); - synchronized (mGlobalLock) { - getDefaultDisplayContentLocked().getPinnedStackController().setAdjustedForShelf(visible, - shelfHeight); - } - } - - @Override public void statusBarVisibilityChanged(int displayId, int visibility) { if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR) != PackageManager.PERMISSION_GRANTED) { diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index 555968a31ca8..479dd1e5d84e 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -50,6 +50,7 @@ import static android.app.admin.DevicePolicyManager.DELEGATION_PACKAGE_ACCESS; import static android.app.admin.DevicePolicyManager.DELEGATION_PERMISSION_GRANT; import static android.app.admin.DevicePolicyManager.ID_TYPE_BASE_INFO; import static android.app.admin.DevicePolicyManager.ID_TYPE_IMEI; +import static android.app.admin.DevicePolicyManager.ID_TYPE_INDIVIDUAL_ATTESTATION; import static android.app.admin.DevicePolicyManager.ID_TYPE_MEID; import static android.app.admin.DevicePolicyManager.ID_TYPE_SERIAL; import static android.app.admin.DevicePolicyManager.LEAVE_ALL_SYSTEM_APPS_ENABLED; @@ -4132,6 +4133,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { final PasswordMetrics metrics = ap.minimumPasswordMetrics; if (metrics.quality != quality) { metrics.quality = quality; + resetInactivePasswordRequirementsIfRPlus(userId, ap); updatePasswordValidityCheckpointLocked(userId, parent); updatePasswordQualityCacheForUserGroup(userId); saveSettingsLocked(userId); @@ -4150,6 +4152,27 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { } /** + * For admins targeting R+ reset various password constraints to default values when quality is + * set to a value that makes those constraints that have no effect. + */ + private void resetInactivePasswordRequirementsIfRPlus(int userId, ActiveAdmin admin) { + if (getTargetSdk(admin.info.getPackageName(), userId) > Build.VERSION_CODES.Q) { + final PasswordMetrics metrics = admin.minimumPasswordMetrics; + if (metrics.quality < PASSWORD_QUALITY_NUMERIC) { + metrics.length = ActiveAdmin.DEF_MINIMUM_PASSWORD_LENGTH; + } + if (metrics.quality < PASSWORD_QUALITY_COMPLEX) { + metrics.letters = ActiveAdmin.DEF_MINIMUM_PASSWORD_LETTERS; + metrics.upperCase = ActiveAdmin.DEF_MINIMUM_PASSWORD_UPPER_CASE; + metrics.lowerCase = ActiveAdmin.DEF_MINIMUM_PASSWORD_LOWER_CASE; + metrics.numeric = ActiveAdmin.DEF_MINIMUM_PASSWORD_NUMERIC; + metrics.symbols = ActiveAdmin.DEF_MINIMUM_PASSWORD_SYMBOLS; + metrics.nonLetter = ActiveAdmin.DEF_MINIMUM_PASSWORD_NON_LETTER; + } + } + } + + /** * Updates a flag that tells us whether the user's password currently satisfies the * requirements set by all of the user's active admins. The flag is updated both in memory * and persisted to disk by calling {@link #saveSettingsLocked}, for the value of the flag @@ -4280,6 +4303,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { ActiveAdmin ap = getActiveAdminForCallerLocked( who, DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD, parent); final PasswordMetrics metrics = ap.minimumPasswordMetrics; + ensureMinimumQuality(userId, ap, PASSWORD_QUALITY_NUMERIC, "setPasswordMinimumLength"); if (metrics.length != length) { metrics.length = length; updatePasswordValidityCheckpointLocked(userId, parent); @@ -4294,10 +4318,19 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { .write(); } + private void ensureMinimumQuality( + int userId, ActiveAdmin admin, int minimumQuality, String operation) { + if (admin.minimumPasswordMetrics.quality < minimumQuality + && getTargetSdk(admin.info.getPackageName(), userId) > Build.VERSION_CODES.Q) { + throw new IllegalStateException(String.format( + "password quality should be at least %d for %s", minimumQuality, operation)); + } + } + @Override public int getPasswordMinimumLength(ComponentName who, int userHandle, boolean parent) { return getStrictestPasswordRequirement(who, userHandle, parent, - admin -> admin.minimumPasswordMetrics.length, PASSWORD_QUALITY_UNSPECIFIED); + admin -> admin.minimumPasswordMetrics.length, PASSWORD_QUALITY_NUMERIC); } @Override @@ -4524,6 +4557,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { synchronized (getLockObject()) { final ActiveAdmin ap = getActiveAdminForCallerLocked( who, DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD, parent); + ensureMinimumQuality( + userId, ap, PASSWORD_QUALITY_COMPLEX, "setPasswordMinimumUpperCase"); final PasswordMetrics metrics = ap.minimumPasswordMetrics; if (metrics.upperCase != length) { metrics.upperCase = length; @@ -4552,6 +4587,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { synchronized (getLockObject()) { ActiveAdmin ap = getActiveAdminForCallerLocked( who, DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD, parent); + ensureMinimumQuality( + userId, ap, PASSWORD_QUALITY_COMPLEX, "setPasswordMinimumLowerCase"); final PasswordMetrics metrics = ap.minimumPasswordMetrics; if (metrics.lowerCase != length) { metrics.lowerCase = length; @@ -4583,6 +4620,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { synchronized (getLockObject()) { ActiveAdmin ap = getActiveAdminForCallerLocked( who, DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD, parent); + ensureMinimumQuality(userId, ap, PASSWORD_QUALITY_COMPLEX, "setPasswordMinimumLetters"); final PasswordMetrics metrics = ap.minimumPasswordMetrics; if (metrics.letters != length) { metrics.letters = length; @@ -4614,6 +4652,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { synchronized (getLockObject()) { ActiveAdmin ap = getActiveAdminForCallerLocked( who, DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD, parent); + ensureMinimumQuality(userId, ap, PASSWORD_QUALITY_COMPLEX, "setPasswordMinimumNumeric"); final PasswordMetrics metrics = ap.minimumPasswordMetrics; if (metrics.numeric != length) { metrics.numeric = length; @@ -4645,6 +4684,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { synchronized (getLockObject()) { ActiveAdmin ap = getActiveAdminForCallerLocked( who, DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD, parent); + ensureMinimumQuality(userId, ap, PASSWORD_QUALITY_COMPLEX, "setPasswordMinimumSymbols"); final PasswordMetrics metrics = ap.minimumPasswordMetrics; if (metrics.symbols != length) { ap.minimumPasswordMetrics.symbols = length; @@ -4676,6 +4716,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { synchronized (getLockObject()) { ActiveAdmin ap = getActiveAdminForCallerLocked( who, DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD, parent); + ensureMinimumQuality( + userId, ap, PASSWORD_QUALITY_COMPLEX, "setPasswordMinimumNonLetter"); final PasswordMetrics metrics = ap.minimumPasswordMetrics; if (metrics.nonLetter != length) { ap.minimumPasswordMetrics.nonLetter = length; @@ -5816,6 +5858,8 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { idTypeToAttestationFlag.put(ID_TYPE_SERIAL, AttestationUtils.ID_TYPE_SERIAL); idTypeToAttestationFlag.put(ID_TYPE_IMEI, AttestationUtils.ID_TYPE_IMEI); idTypeToAttestationFlag.put(ID_TYPE_MEID, AttestationUtils.ID_TYPE_MEID); + idTypeToAttestationFlag.put( + ID_TYPE_INDIVIDUAL_ATTESTATION, AttestationUtils.USE_INDIVIDUAL_ATTESTATION); int numFlagsSet = Integer.bitCount(idAttestationFlags); // No flags are set - return null to indicate no device ID attestation information should @@ -8008,6 +8052,10 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { "clearDeviceOwner can only be called by the device owner"); } enforceUserUnlocked(deviceOwnerUserId); + DevicePolicyData policy = getUserData(deviceOwnerUserId); + if (policy.mPasswordTokenHandle != 0) { + mLockPatternUtils.removeEscrowToken(policy.mPasswordTokenHandle, deviceOwnerUserId); + } final ActiveAdmin admin = getDeviceOwnerAdminLocked(); long ident = mInjector.binderClearCallingIdentity(); diff --git a/services/tests/servicestests/src/com/android/server/SystemConfigTest.java b/services/tests/servicestests/src/com/android/server/SystemConfigTest.java new file mode 100644 index 000000000000..ff03391ea031 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/SystemConfigTest.java @@ -0,0 +1,180 @@ +/* + * Copyright (C) 2019 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.server; + +import static org.junit.Assert.assertEquals; + +import android.util.ArrayMap; +import android.util.ArraySet; +import android.util.Log; + +import androidx.test.filters.SmallTest; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Arrays; +import java.util.Map; +import java.util.Scanner; +import java.util.Set; + +/** + * Tests for {@link SystemConfig}. + * + * Build/Install/Run: + * atest FrameworksServicesTests:SystemConfigTest + */ +@RunWith(AndroidJUnit4.class) +@SmallTest +public class SystemConfigTest { + private static final String LOG_TAG = "SystemConfigTest"; + + private SystemConfig mSysConfig; + + @Rule public TemporaryFolder mTemporaryFolder = new TemporaryFolder(); + + @Before + public void setUp() throws Exception { + mSysConfig = new SystemConfigTestClass(); + } + + /** + * Subclass of SystemConfig without running the constructor. + */ + private class SystemConfigTestClass extends SystemConfig { + SystemConfigTestClass() { + super(false); + } + } + + /** + * Tests that readPermissions works correctly for the tag: install-in-user-type + */ + @Test + public void testInstallInUserType() throws Exception { + final String contents1 = + "<permissions>\n" + + " <install-in-user-type package=\"com.android.package1\">\n" + + " <install-in user-type=\"FULL\" />\n" + + " <install-in user-type=\"PROFILE\" />\n" + + " </install-in-user-type>\n" + + " <install-in-user-type package=\"com.android.package2\">\n" + + " <install-in user-type=\"FULL\" />\n" + + " <install-in user-type=\"PROFILE\" />\n" + + " <do-not-install-in user-type=\"GUEST\" />\n" + + " </install-in-user-type>\n" + + "</permissions>"; + + final String contents2 = + "<permissions>\n" + + " <install-in-user-type package=\"com.android.package2\">\n" + + " <install-in user-type=\"SYSTEM\" />\n" + + " <do-not-install-in user-type=\"PROFILE\" />\n" + + " </install-in-user-type>\n" + + "</permissions>"; + + final String contents3 = + "<permissions>\n" + + " <install-in-user-type package=\"com.android.package2\">\n" + + " <install-in invalid-attribute=\"ADMIN\" />\n" // Ignore invalid attribute + + " </install-in-user-type>\n" + + " <install-in-user-type package=\"com.android.package2\">\n" + + " <install-in user-type=\"RESTRICTED\" />\n" // Valid + + " </install-in-user-type>\n" + + " <install-in-user-type>\n" // Ignored since missing package name + + " <install-in user-type=\"ADMIN\" />\n" + + " </install-in-user-type>\n" + + "</permissions>"; + + Map<String, Set<String>> expectedWhite = new ArrayMap<>(); + expectedWhite.put("com.android.package1", + new ArraySet<>(Arrays.asList("FULL", "PROFILE"))); + expectedWhite.put("com.android.package2", + new ArraySet<>(Arrays.asList("FULL", "PROFILE", "RESTRICTED", "SYSTEM"))); + + Map<String, Set<String>> expectedBlack = new ArrayMap<>(); + expectedBlack.put("com.android.package2", + new ArraySet<>(Arrays.asList("GUEST", "PROFILE"))); + + final File folder1 = createTempSubfolder("folder1"); + createTempFile(folder1, "permFile1.xml", contents1); + + final File folder2 = createTempSubfolder("folder2"); + createTempFile(folder2, "permFile2.xml", contents2); + + // Also, make a third file, but with the name folder1/permFile2.xml, to prove no conflicts. + createTempFile(folder1, "permFile2.xml", contents3); + + mSysConfig.readPermissions(folder1, /* No permission needed anyway */ 0); + mSysConfig.readPermissions(folder2, /* No permission needed anyway */ 0); + + Map<String, Set<String>> actualWhite = mSysConfig.getAndClearPackageToUserTypeWhitelist(); + Map<String, Set<String>> actualBlack = mSysConfig.getAndClearPackageToUserTypeBlacklist(); + + assertEquals("Whitelist was not cleared", 0, + mSysConfig.getAndClearPackageToUserTypeWhitelist().size()); + assertEquals("Blacklist was not cleared", 0, + mSysConfig.getAndClearPackageToUserTypeBlacklist().size()); + + assertEquals("Incorrect whitelist.", expectedWhite, actualWhite); + assertEquals("Incorrect blacklist", expectedBlack, actualBlack); + } + + /** + * Creates folderName/fileName in the mTemporaryFolder and fills it with the contents. + * @param folderName subdirectory of mTemporaryFolder to put the file, creating if needed + * @return the folder + */ + private File createTempSubfolder(String folderName) + throws IOException { + File folder = new File(mTemporaryFolder.getRoot(), folderName); + folder.mkdir(); + return folder; + } + + /** + * Creates folderName/fileName in the mTemporaryFolder and fills it with the contents. + * @param folder pre-existing subdirectory of mTemporaryFolder to put the file + * @param fileName name of the file (e.g. filename.xml) to create + * @param contents contents to write to the file + * @return the folder containing the newly created file (not the file itself!) + */ + private File createTempFile(File folder, String fileName, String contents) + throws IOException { + File file = new File(folder, fileName); + BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + bw.write(contents); + bw.close(); + + // Print to logcat for test debugging. + Log.d(LOG_TAG, "Contents of file " + file.getAbsolutePath()); + Scanner input = new Scanner(file); + while (input.hasNextLine()) { + Log.d(LOG_TAG, input.nextLine()); + } + + return folder; + } +} diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java index d90091017116..a25e40f8cc13 100644 --- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java @@ -1212,6 +1212,45 @@ public class DevicePolicyManagerTest extends DpmTestBase { assertTrue(dpm.isDeviceManaged()); } + /** + * Test for: {@link DevicePolicyManager#clearDeviceOwnerApp(String)} + * + * Validates that when the device owner is removed, the reset password token is cleared + */ + public void testClearDeviceOwner_clearResetPasswordToken() throws Exception { + mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); + mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS); + mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID; + + // Install admin1 on system user + setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID); + + // Set admin1 to active admin and device owner + dpm.setActiveAdmin(admin1, /* replace =*/ false); + dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM); + + // Add reset password token + final long handle = 12000; + final byte[] token = new byte[32]; + when(getServices().lockPatternUtils.addEscrowToken(eq(token), eq(UserHandle.USER_SYSTEM), + nullable(EscrowTokenStateChangeCallback.class))) + .thenReturn(handle); + assertTrue(dpm.setResetPasswordToken(admin1, token)); + + // Assert reset password token is active + when(getServices().lockPatternUtils.isEscrowTokenActive(eq(handle), + eq(UserHandle.USER_SYSTEM))) + .thenReturn(true); + assertTrue(dpm.isResetPasswordTokenActive(admin1)); + + // Remove the device owner + dpm.clearDeviceOwnerApp(admin1.getPackageName()); + + // Verify password reset password token was removed + verify(getServices().lockPatternUtils).removeEscrowToken(eq(handle), + eq(UserHandle.USER_SYSTEM)); + } + public void testSetProfileOwner() throws Exception { setAsProfileOwner(admin1); diff --git a/services/tests/servicestests/src/com/android/server/display/whitebalance/AmbientFilterTest.java b/services/tests/servicestests/src/com/android/server/display/utils/AmbientFilterTest.java index 78164939aa49..9b76b13d2ede 100644 --- a/services/tests/servicestests/src/com/android/server/display/whitebalance/AmbientFilterTest.java +++ b/services/tests/servicestests/src/com/android/server/display/utils/AmbientFilterTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.server.display.whitebalance; +package com.android.server.display.utils; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; @@ -38,6 +38,7 @@ import org.junit.runners.JUnit4; public final class AmbientFilterTest { private ContextWrapper mContextSpy; private Resources mResourcesSpy; + private static String TAG = "AmbientFilterTest"; @Before public void setUp() throws Exception { @@ -54,7 +55,7 @@ public final class AmbientFilterTest { final int prediction_time = 100; // Hardcoded in AmbientFilter: prediction of how long the // latest prediction will last before a new prediction. setMockValues(mResourcesSpy, horizon, intercept); - AmbientFilter filter = DisplayWhiteBalanceFactory.createBrightnessFilter(mResourcesSpy); + AmbientFilter filter = AmbientFilterFactory.createBrightnessFilter(TAG, mResourcesSpy); // Add first value and verify filter.addValue(time_start, 30); @@ -85,7 +86,7 @@ public final class AmbientFilterTest { final int prediction_time = 100; setMockValues(mResourcesSpy, horizon, intercept); - AmbientFilter filter = DisplayWhiteBalanceFactory.createBrightnessFilter(mResourcesSpy); + AmbientFilter filter = AmbientFilterFactory.createBrightnessFilter(TAG, mResourcesSpy); // Add first value and verify filter.addValue(time_start, 30); diff --git a/services/tests/servicestests/src/com/android/server/display/whitebalance/AmbientFilterStubber.java b/services/tests/servicestests/src/com/android/server/display/whitebalance/AmbientFilterStubber.java new file mode 100644 index 000000000000..4d2551087c59 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/display/whitebalance/AmbientFilterStubber.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2019 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.server.display.utils; + +public class AmbientFilterStubber extends AmbientFilter { + public AmbientFilterStubber() { + super(null, 1); + } + + protected float filter(long time, RollingBuffer buffer) { + return 0f; + } +} diff --git a/services/tests/servicestests/src/com/android/server/display/whitebalance/AmbientLuxTest.java b/services/tests/servicestests/src/com/android/server/display/whitebalance/AmbientLuxTest.java index 6b0798bdce22..0d5a7d6c1952 100644 --- a/services/tests/servicestests/src/com/android/server/display/whitebalance/AmbientLuxTest.java +++ b/services/tests/servicestests/src/com/android/server/display/whitebalance/AmbientLuxTest.java @@ -17,6 +17,8 @@ package com.android.server.display.whitebalance; import com.android.internal.R; +import com.android.server.display.utils.AmbientFilter; +import com.android.server.display.utils.AmbientFilterStubber; import com.google.common.collect.ImmutableList; import static org.junit.Assert.assertEquals; @@ -132,7 +134,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = 8000.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); for (float luxOverride = 0.1f; luxOverride <= 10000; luxOverride *= 10) { setEstimatedBrightnessAndUpdate(controller, luxOverride); @@ -152,7 +154,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = 8000.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); for (float t = 0.0f; t <= 1.0f; t += 0.1f) { setEstimatedBrightnessAndUpdate(controller, @@ -184,7 +186,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = 8000.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); for (float t = 0.0f; t <= 1.0f; t += 0.1f) { float luxOverride = mix(brightness0, brightness1, t); @@ -221,7 +223,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = 8000.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); setEstimatedBrightnessAndUpdate(controller, 0.0f); assertEquals(controller.mPendingAmbientColorTemperature, @@ -240,7 +242,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = 8000.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); for (float luxOverride = 0.1f; luxOverride <= 10000; luxOverride *= 10) { setEstimatedBrightnessAndUpdate(controller, luxOverride); @@ -258,7 +260,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = 8000.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); for (float luxOverride = 0.1f; luxOverride <= 10000; luxOverride *= 10) { setEstimatedBrightnessAndUpdate(controller, luxOverride); @@ -278,7 +280,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = 8000.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); for (float t = 0.0f; t <= 1.0f; t += 0.1f) { setEstimatedBrightnessAndUpdate(controller, @@ -311,7 +313,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = 6000.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); for (float t = 0.0f; t <= 1.0f; t += 0.1f) { float luxOverride = mix(brightness0, brightness1, t); @@ -350,7 +352,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = 8000.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); for (float luxOverride = 0.1f; luxOverride <= 10000; luxOverride *= 10) { setEstimatedBrightnessAndUpdate(controller, luxOverride); @@ -370,7 +372,7 @@ public final class AmbientLuxTest { DisplayWhiteBalanceFactory.create(mHandler, mSensorManagerMock, mResourcesSpy); final float ambientColorTemperature = -1.0f; setEstimatedColorTemperature(controller, ambientColorTemperature); - controller.mBrightnessFilter = spy(controller.mBrightnessFilter); + controller.mBrightnessFilter = spy(new AmbientFilterStubber()); for (float t = 0.0f; t <= 1.0f; t += 0.1f) { setEstimatedBrightnessAndUpdate(controller, @@ -426,7 +428,7 @@ public final class AmbientLuxTest { private void setEstimatedColorTemperature(DisplayWhiteBalanceController controller, float ambientColorTemperature) { - AmbientFilter colorTemperatureFilter = spy(controller.mColorTemperatureFilter); + AmbientFilter colorTemperatureFilter = spy(new AmbientFilterStubber()); controller.mColorTemperatureFilter = colorTemperatureFilter; when(colorTemperatureFilter.getEstimate(anyLong())).thenReturn(ambientColorTemperature); } diff --git a/services/tests/servicestests/src/com/android/server/pm/UserSystemPackageInstallerTest.java b/services/tests/servicestests/src/com/android/server/pm/UserSystemPackageInstallerTest.java new file mode 100644 index 000000000000..f0b0328ff7d4 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/pm/UserSystemPackageInstallerTest.java @@ -0,0 +1,396 @@ +/* + * Copyright (C) 2019 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.server.pm; + +import static android.content.pm.UserInfo.FLAG_FULL; +import static android.content.pm.UserInfo.FLAG_GUEST; +import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE; +import static android.content.pm.UserInfo.FLAG_SYSTEM; + +import static com.android.server.pm.UserSystemPackageInstaller.PACKAGE_WHITELIST_MODE_PROP; +import static com.android.server.pm.UserSystemPackageInstaller.USER_TYPE_PACKAGE_WHITELIST_MODE_DEVICE_DEFAULT; +import static com.android.server.pm.UserSystemPackageInstaller.USER_TYPE_PACKAGE_WHITELIST_MODE_DISABLE; +import static com.android.server.pm.UserSystemPackageInstaller.USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE; +import static com.android.server.pm.UserSystemPackageInstaller.USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST; +import static com.android.server.pm.UserSystemPackageInstaller.USER_TYPE_PACKAGE_WHITELIST_MODE_LOG; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import android.content.Context; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageParser; +import android.content.pm.UserInfo; +import android.os.Looper; +import android.os.SystemProperties; +import android.os.UserManager; +import android.os.UserManagerInternal; +import android.support.test.uiautomator.UiDevice; +import android.util.ArrayMap; +import android.util.ArraySet; +import android.util.Log; + +import androidx.test.InstrumentationRegistry; +import androidx.test.filters.MediumTest; +import androidx.test.runner.AndroidJUnit4; + +import com.android.server.LocalServices; +import com.android.server.SystemConfig; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +/** + * Tests for UserSystemPackageInstaller. + * + * <p>Run with:<pre> + * atest com.android.server.pm.UserSystemPackageInstallerTest + * </pre> + */ +@RunWith(AndroidJUnit4.class) +@MediumTest +public class UserSystemPackageInstallerTest { + private static final String TAG = "UserSystemPackageInstallerTest"; + + private UserSystemPackageInstaller mUserSystemPackageInstaller; + + private Context mContext; + + /** Any users created during this test, for them to be removed when it's done. */ + private final List<Integer> mRemoveUsers = new ArrayList<>(); + /** Original value of PACKAGE_WHITELIST_MODE_PROP before the test, to reset at end. */ + private final int mOriginalWhitelistMode = SystemProperties.getInt( + PACKAGE_WHITELIST_MODE_PROP, USER_TYPE_PACKAGE_WHITELIST_MODE_DEVICE_DEFAULT); + + @Before + public void setup() { + // Currently UserManagerService cannot be instantiated twice inside a VM without a cleanup + // TODO: Remove once UMS supports proper dependency injection + if (Looper.myLooper() == null) { + Looper.prepare(); + } + LocalServices.removeServiceForTest(UserManagerInternal.class); + UserManagerService ums = new UserManagerService(InstrumentationRegistry.getContext()); + + mUserSystemPackageInstaller = new UserSystemPackageInstaller(ums); + mContext = InstrumentationRegistry.getTargetContext(); + } + + @After + public void tearDown() { + UserManager um = UserManager.get(mContext); + for (int userId : mRemoveUsers) { + um.removeUser(userId); + } + setUserTypePackageWhitelistMode(mOriginalWhitelistMode); + } + + /** + * Subclass of SystemConfig without running the constructor. + */ + private class SystemConfigTestClass extends SystemConfig { + SystemConfigTestClass(boolean readPermissions) { + super(readPermissions); + } + } + + /** + * Test that determineWhitelistedPackagesForUserTypes reads SystemConfig information properly. + */ + @Test + public void testDetermineWhitelistedPackagesForUserTypes() { + SystemConfig sysConfig = new SystemConfigTestClass(false) { + @Override + public ArrayMap<String, Set<String>> getAndClearPackageToUserTypeWhitelist() { + ArrayMap<String, Set<String>> r = new ArrayMap<>(); + r.put("com.android.package1", new ArraySet<>(Arrays.asList( + "PROFILE", "SYSTEM", "GUEST", "FULL", "invalid-garbage1"))); + r.put("com.android.package2", new ArraySet<>(Arrays.asList( + "MANAGED_PROFILE"))); + return r; + } + + @Override + public ArrayMap<String, Set<String>> getAndClearPackageToUserTypeBlacklist() { + ArrayMap<String, Set<String>> r = new ArrayMap<>(); + r.put("com.android.package1", new ArraySet<>(Arrays.asList( + "FULL", "RESTRICTED", "invalid-garbage2"))); + return r; + } + }; + + final ArrayMap<String, Integer> expectedOutput = getNewPackageToWhitelistedFlagsMap(); + expectedOutput.put("com.android.package1", + UserInfo.PROFILE_FLAGS_MASK | FLAG_SYSTEM | FLAG_GUEST); + expectedOutput.put("com.android.package2", + UserInfo.FLAG_MANAGED_PROFILE); + + final ArrayMap<String, Integer> actualOutput = + mUserSystemPackageInstaller.determineWhitelistedPackagesForUserTypes(sysConfig); + + assertEquals("Incorrect package-to-user mapping.", expectedOutput, actualOutput); + } + + /** + * Test that determineWhitelistedPackagesForUserTypes does not include packages that were never + * whitelisted properly, but does include packages that were whitelisted but then blacklisted. + */ + @Test + public void testDetermineWhitelistedPackagesForUserTypes_noNetWhitelisting() { + SystemConfig sysConfig = new SystemConfigTestClass(false) { + @Override + public ArrayMap<String, Set<String>> getAndClearPackageToUserTypeWhitelist() { + ArrayMap<String, Set<String>> r = new ArrayMap<>(); + r.put("com.android.package1", new ArraySet<>(Arrays.asList("invalid1"))); + // com.android.package2 has no whitelisting + r.put("com.android.package3", new ArraySet<>(Arrays.asList("PROFILE", "FULL"))); + r.put("com.android.package4", new ArraySet<>(Arrays.asList("PROFILE"))); + r.put("com.android.package5", new ArraySet<>()); + // com.android.package6 has no whitelisting + return r; + } + + @Override + public ArrayMap<String, Set<String>> getAndClearPackageToUserTypeBlacklist() { + ArrayMap<String, Set<String>> r = new ArrayMap<>(); + // com.android.package1 has no blacklisting + r.put("com.android.package2", new ArraySet<>(Arrays.asList("FULL"))); + r.put("com.android.package3", new ArraySet<>(Arrays.asList("PROFILE", "FULL"))); + r.put("com.android.package4", new ArraySet<>(Arrays.asList("PROFILE", "invalid4"))); + // com.android.package5 has no blacklisting + r.put("com.android.package6", new ArraySet<>(Arrays.asList("invalid6"))); + return r; + } + }; + + final ArrayMap<String, Integer> expectedOutput = getNewPackageToWhitelistedFlagsMap(); + expectedOutput.put("com.android.package3", 0); + expectedOutput.put("com.android.package4", 0); + + final ArrayMap<String, Integer> actualOutput = + mUserSystemPackageInstaller.determineWhitelistedPackagesForUserTypes(sysConfig); + + assertEquals("Incorrect package-to-user mapping.", expectedOutput, actualOutput); + } + + /** + * Tests that shouldInstallPackage correctly determines which packages should be installed. + */ + @Test + public void testShouldInstallPackage() { + final String packageName1 = "pkg1"; // whitelisted + final String packageName2 = "pkg2"; // whitelisted and blacklisted + final String packageName3 = "pkg3"; // whitelisted for a different user type + final String packageName4 = "pkg4"; // not whitelisted nor blacklisted at all + + final ArrayMap<String, Integer> pkgFlgMap = new ArrayMap<>(); // Whitelist: pkgs per flags + pkgFlgMap.put(packageName1, FLAG_FULL); + pkgFlgMap.put(packageName2, 0); + pkgFlgMap.put(packageName3, FLAG_MANAGED_PROFILE); + + // Whitelist of pkgs for this specific user, i.e. subset of pkgFlagMap for this user. + final Set<String> userWhitelist = new ArraySet<>(); + userWhitelist.add(packageName1); + + final UserSystemPackageInstaller uspi = new UserSystemPackageInstaller(null, pkgFlgMap); + + final PackageParser.Package pkg1 = new PackageParser.Package(packageName1); + final PackageParser.Package pkg2 = new PackageParser.Package(packageName2); + final PackageParser.Package pkg3 = new PackageParser.Package(packageName3); + final PackageParser.Package pkg4 = new PackageParser.Package(packageName4); + + // No implicit whitelist, so only install pkg1. + boolean implicit = false; + boolean isSysUser = false; + assertTrue(uspi.shouldInstallPackage(pkg1, pkgFlgMap, userWhitelist, implicit, isSysUser)); + assertFalse(uspi.shouldInstallPackage(pkg2, pkgFlgMap, userWhitelist, implicit, isSysUser)); + assertFalse(uspi.shouldInstallPackage(pkg3, pkgFlgMap, userWhitelist, implicit, isSysUser)); + assertFalse(uspi.shouldInstallPackage(pkg4, pkgFlgMap, userWhitelist, implicit, isSysUser)); + + // Use implicit whitelist, so install pkg1 and pkg4 + implicit = true; + isSysUser = false; + assertTrue(uspi.shouldInstallPackage(pkg1, pkgFlgMap, userWhitelist, implicit, isSysUser)); + assertFalse(uspi.shouldInstallPackage(pkg2, pkgFlgMap, userWhitelist, implicit, isSysUser)); + assertFalse(uspi.shouldInstallPackage(pkg3, pkgFlgMap, userWhitelist, implicit, isSysUser)); + assertTrue(uspi.shouldInstallPackage(pkg4, pkgFlgMap, userWhitelist, implicit, isSysUser)); + + // For user 0 specifically, we always implicitly whitelist. + implicit = false; + isSysUser = true; + assertTrue(uspi.shouldInstallPackage(pkg1, pkgFlgMap, userWhitelist, implicit, isSysUser)); + assertFalse(uspi.shouldInstallPackage(pkg2, pkgFlgMap, userWhitelist, implicit, isSysUser)); + assertFalse(uspi.shouldInstallPackage(pkg3, pkgFlgMap, userWhitelist, implicit, isSysUser)); + assertTrue(uspi.shouldInstallPackage(pkg4, pkgFlgMap, userWhitelist, implicit, isSysUser)); + } + + /** + * Tests that getWhitelistedPackagesForUserType works properly, assuming that + * mWhitelistedPackagesForUserTypes (i.e. determineWhitelistedPackagesForUserTypes) is correct. + */ + @Test + public void testGetWhitelistedPackagesForUserType() { + final String packageName1 = "pkg1"; // whitelisted for FULL + final String packageName2 = "pkg2"; // blacklisted whenever whitelisted + final String packageName3 = "pkg3"; // whitelisted for SYSTEM + final String packageName4 = "pkg4"; // whitelisted for FULL + + final ArrayMap<String, Integer> pkgFlagMap = new ArrayMap<>(); // Whitelist: pkgs per flags + pkgFlagMap.put(packageName1, FLAG_FULL); + pkgFlagMap.put(packageName2, 0); + pkgFlagMap.put(packageName3, FLAG_SYSTEM); + pkgFlagMap.put(packageName4, FLAG_FULL); + + // Whitelist of pkgs for this specific user, i.e. subset of pkgFlagMap for this user. + final Set<String> expectedUserWhitelist = new ArraySet<>(); + expectedUserWhitelist.add(packageName1); + + UserSystemPackageInstaller uspi = new UserSystemPackageInstaller(null, pkgFlagMap); + + Set<String> output = uspi.getWhitelistedPackagesForUserType(FLAG_FULL); + assertEquals("Whitelist for FULL is the wrong size", 2, output.size()); + assertTrue("Whitelist for FULL doesn't contain pkg1", output.contains(packageName1)); + assertTrue("Whitelist for FULL doesn't contain pkg4", output.contains(packageName4)); + + output = uspi.getWhitelistedPackagesForUserType(FLAG_SYSTEM); + assertEquals("Whitelist for SYSTEM is the wrong size", 1, output.size()); + assertTrue("Whitelist for SYSTEM doesn't contain pkg1", output.contains(packageName3)); + } + + /** + * Test that a newly created FULL user has the expected system packages. + * + * Assumes that SystemConfig and UserManagerService.determineWhitelistedPackagesForUserTypes + * work correctly (they are tested separately). + */ + @Test + public void testPackagesForCreateUser_full() { + final int userFlags = UserInfo.FLAG_FULL; + setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE); + PackageManager pm = mContext.getPackageManager(); + + final SystemConfig sysConfig = new SystemConfigTestClass(true); + final ArrayMap<String, Integer> packageMap = + mUserSystemPackageInstaller.determineWhitelistedPackagesForUserTypes(sysConfig); + final Set<String> expectedPackages = new ArraySet<>(packageMap.size()); + for (int i = 0; i < packageMap.size(); i++) { + if ((userFlags & packageMap.valueAt(i)) != 0) { + expectedPackages.add(packageMap.keyAt(i)); + } + } + + final UserManager um = UserManager.get(mContext); + final UserInfo user = um.createUser("Test User", userFlags); + assertNotNull(user); + mRemoveUsers.add(user.id); + + final List<PackageInfo> packageInfos = pm.getInstalledPackagesAsUser( + PackageManager.MATCH_SYSTEM_ONLY + | PackageManager.MATCH_DISABLED_COMPONENTS + | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, + user.id); + final Set<String> actualPackages = new ArraySet<>(packageInfos.size()); + for (PackageInfo p : packageInfos) { + actualPackages.add(p.packageName); + } + checkPackageDifferences(expectedPackages, actualPackages); + } + + /** Asserts that actual is a subset of expected. */ + private void checkPackageDifferences(Set<String> expected, Set<String> actual) { + final Set<String> uniqueToExpected = new ArraySet<>(expected); + uniqueToExpected.removeAll(actual); + final Set<String> uniqueToActual = new ArraySet<>(actual); + uniqueToActual.removeAll(expected); + + Log.v(TAG, "Expected list uniquely has " + uniqueToExpected); + Log.v(TAG, "Actual list uniquely has " + uniqueToActual); + + assertTrue("User's system packages includes non-whitelisted packages: " + uniqueToActual, + uniqueToActual.isEmpty()); + } + + /** + * Test that setEnableUserTypePackageWhitelist() has the correct effect. + */ + @Test + public void testSetWhitelistEnabledMode() { + setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_DISABLE); + assertFalse(mUserSystemPackageInstaller.isLogMode()); + assertFalse(mUserSystemPackageInstaller.isEnforceMode()); + assertFalse(mUserSystemPackageInstaller.isImplicitWhitelistMode()); + + setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_LOG); + assertTrue(mUserSystemPackageInstaller.isLogMode()); + assertFalse(mUserSystemPackageInstaller.isEnforceMode()); + assertFalse(mUserSystemPackageInstaller.isImplicitWhitelistMode()); + + setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE); + assertFalse(mUserSystemPackageInstaller.isLogMode()); + assertTrue(mUserSystemPackageInstaller.isEnforceMode()); + assertFalse(mUserSystemPackageInstaller.isImplicitWhitelistMode()); + + setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST); + assertFalse(mUserSystemPackageInstaller.isLogMode()); + assertFalse(mUserSystemPackageInstaller.isEnforceMode()); + assertTrue(mUserSystemPackageInstaller.isImplicitWhitelistMode()); + + setUserTypePackageWhitelistMode( + USER_TYPE_PACKAGE_WHITELIST_MODE_LOG | USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE); + assertTrue(mUserSystemPackageInstaller.isLogMode()); + assertTrue(mUserSystemPackageInstaller.isEnforceMode()); + assertFalse(mUserSystemPackageInstaller.isImplicitWhitelistMode()); + + setUserTypePackageWhitelistMode(USER_TYPE_PACKAGE_WHITELIST_MODE_IMPLICIT_WHITELIST + | USER_TYPE_PACKAGE_WHITELIST_MODE_ENFORCE); + assertFalse(mUserSystemPackageInstaller.isLogMode()); + assertTrue(mUserSystemPackageInstaller.isEnforceMode()); + assertTrue(mUserSystemPackageInstaller.isImplicitWhitelistMode()); + } + + /** Sets the whitelist mode to the desired value via adb's setprop. */ + private void setUserTypePackageWhitelistMode(int mode) { + UiDevice mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); + try { + String result = mUiDevice.executeShellCommand(String.format("setprop %s %d", + PACKAGE_WHITELIST_MODE_PROP, mode)); + assertFalse("Failed to set sysprop " + PACKAGE_WHITELIST_MODE_PROP + ": " + result, + result != null && result.contains("Failed")); + } catch (IOException e) { + fail("Failed to set sysprop " + PACKAGE_WHITELIST_MODE_PROP + ":\n" + e); + } + } + + private ArrayMap<String, Integer> getNewPackageToWhitelistedFlagsMap() { + final ArrayMap<String, Integer> pkgFlagMap = new ArrayMap<>(); + // "android" is always treated as whitelisted, regardless of the xml file. + pkgFlagMap.put("android", FLAG_SYSTEM | UserInfo.FLAG_FULL | UserInfo.PROFILE_FLAGS_MASK); + return pkgFlagMap; + } +} diff --git a/services/tests/servicestests/src/com/android/server/protolog/ProtoLogImplTest.java b/services/tests/servicestests/src/com/android/server/protolog/ProtoLogImplTest.java index 7aa3d0dfdd1f..3e9f625ecdd9 100644 --- a/services/tests/servicestests/src/com/android/server/protolog/ProtoLogImplTest.java +++ b/services/tests/servicestests/src/com/android/server/protolog/ProtoLogImplTest.java @@ -351,7 +351,7 @@ public class ProtoLogImplTest { ProtoLogData data = readProtoLogSingle(ip); assertNotNull(data); assertEquals(1234, data.mMessageHash.longValue()); - assertTrue(before < data.mElapsedTime && data.mElapsedTime < after); + assertTrue(before <= data.mElapsedTime && data.mElapsedTime <= after); assertArrayEquals(new String[]{"test"}, data.mStrParams.toArray()); assertArrayEquals(new Long[]{1L, 2L, 3L}, data.mSint64Params.toArray()); assertArrayEquals(new Double[]{0.4, 0.5, 0.6}, data.mDoubleParams.toArray()); @@ -376,7 +376,7 @@ public class ProtoLogImplTest { ProtoLogData data = readProtoLogSingle(ip); assertNotNull(data); assertEquals(1234, data.mMessageHash.longValue()); - assertTrue(before < data.mElapsedTime && data.mElapsedTime < after); + assertTrue(before <= data.mElapsedTime && data.mElapsedTime <= after); assertArrayEquals(new String[]{"test", "(INVALID PARAMS_MASK) true"}, data.mStrParams.toArray()); assertArrayEquals(new Long[]{1L}, data.mSint64Params.toArray()); diff --git a/services/tests/servicestests/src/com/android/server/stats/ProcfsMemoryUtilTest.java b/services/tests/servicestests/src/com/android/server/stats/ProcfsMemoryUtilTest.java index ae5777403528..dd2ee5cce13b 100644 --- a/services/tests/servicestests/src/com/android/server/stats/ProcfsMemoryUtilTest.java +++ b/services/tests/servicestests/src/com/android/server/stats/ProcfsMemoryUtilTest.java @@ -16,7 +16,6 @@ package com.android.server.stats; import static com.android.server.stats.ProcfsMemoryUtil.parseMemorySnapshotFromStatus; -import static com.android.server.stats.ProcfsMemoryUtil.parseVmHWMFromStatus; import static com.google.common.truth.Truth.assertThat; @@ -80,45 +79,25 @@ public class ProcfsMemoryUtilTest { + "nonvoluntary_ctxt_switches:\t104\n"; @Test - public void testParseVmHWMFromStatus_parsesCorrectValue() { - assertThat(parseVmHWMFromStatus(STATUS_CONTENTS)).isEqualTo(137668); - } - - @Test - public void testParseVmHWMFromStatus_invalidValue() { - assertThat(parseVmHWMFromStatus("test\nVmHWM: x0x0x\ntest")).isEqualTo(0); - } - - @Test - public void testParseVmHWMFromStatus_emptyContents() { - assertThat(parseVmHWMFromStatus("")).isEqualTo(0); - } - - @Test public void testParseMemorySnapshotFromStatus_parsesCorrectValue() { MemorySnapshot snapshot = parseMemorySnapshotFromStatus(STATUS_CONTENTS); + assertThat(snapshot.uid).isEqualTo(10083); + assertThat(snapshot.rssHighWaterMarkInKilobytes).isEqualTo(137668); assertThat(snapshot.rssInKilobytes).isEqualTo(126776); assertThat(snapshot.anonRssInKilobytes).isEqualTo(37860); assertThat(snapshot.swapInKilobytes).isEqualTo(22); - assertThat(snapshot.isEmpty()).isFalse(); } @Test public void testParseMemorySnapshotFromStatus_invalidValue() { MemorySnapshot snapshot = parseMemorySnapshotFromStatus("test\nVmRSS:\tx0x0x\nVmSwap:\t1 kB\ntest"); - assertThat(snapshot.rssInKilobytes).isEqualTo(0); - assertThat(snapshot.anonRssInKilobytes).isEqualTo(0); - assertThat(snapshot.swapInKilobytes).isEqualTo(1); - assertThat(snapshot.isEmpty()).isFalse(); + assertThat(snapshot).isNull(); } @Test public void testParseMemorySnapshotFromStatus_emptyContents() { MemorySnapshot snapshot = parseMemorySnapshotFromStatus(""); - assertThat(snapshot.rssInKilobytes).isEqualTo(0); - assertThat(snapshot.anonRssInKilobytes).isEqualTo(0); - assertThat(snapshot.swapInKilobytes).isEqualTo(0); - assertThat(snapshot.isEmpty()).isTrue(); + assertThat(snapshot).isNull(); } } diff --git a/services/tests/wmtests/src/com/android/server/wm/PinnedStackControllerTest.java b/services/tests/wmtests/src/com/android/server/wm/PinnedStackControllerTest.java deleted file mode 100644 index e9c226340164..000000000000 --- a/services/tests/wmtests/src/com/android/server/wm/PinnedStackControllerTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2018 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.server.wm; - -import static android.view.Display.DEFAULT_DISPLAY; - -import static com.android.dx.mockito.inline.extended.ExtendedMockito.never; -import static com.android.dx.mockito.inline.extended.ExtendedMockito.reset; -import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; -import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.eq; - -import android.os.RemoteException; -import android.platform.test.annotations.Presubmit; -import android.view.IPinnedStackListener; - -import androidx.test.filters.SmallTest; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -/** - * Build/Install/Run: - * atest FrameworksServicesTests:PinnedStackControllerTest - */ -@SmallTest -@Presubmit -public class PinnedStackControllerTest extends WindowTestsBase { - - private static final int SHELF_HEIGHT = 300; - - @Mock private IPinnedStackListener mIPinnedStackListener; - @Mock private IPinnedStackListener.Stub mIPinnedStackListenerStub; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - - when(mIPinnedStackListener.asBinder()).thenReturn(mIPinnedStackListenerStub); - } - - @Test - public void setShelfHeight_shelfVisibilityChangedTriggered() throws RemoteException { - mWm.mAtmService.mSupportsPictureInPicture = true; - mWm.registerPinnedStackListener(DEFAULT_DISPLAY, mIPinnedStackListener); - - verify(mIPinnedStackListener).onImeVisibilityChanged(false, 0); - verify(mIPinnedStackListener).onShelfVisibilityChanged(false, 0); - verify(mIPinnedStackListener).onMovementBoundsChanged(any(), eq(false), - eq(false)); - verify(mIPinnedStackListener).onActionsChanged(any()); - verify(mIPinnedStackListener).onMinimizedStateChanged(anyBoolean()); - - reset(mIPinnedStackListener); - - mWm.setShelfHeight(true, SHELF_HEIGHT); - verify(mIPinnedStackListener).onShelfVisibilityChanged(true, SHELF_HEIGHT); - verify(mIPinnedStackListener).onMovementBoundsChanged(any(), eq(false), - eq(true)); - verify(mIPinnedStackListener, never()).onImeVisibilityChanged(anyBoolean(), anyInt()); - } -} diff --git a/services/tests/wmtests/src/com/android/server/wm/ProtoLogIntegrationTest.java b/services/tests/wmtests/src/com/android/server/wm/ProtoLogIntegrationTest.java index 8eecff532ad9..acbbc461e4dd 100644 --- a/services/tests/wmtests/src/com/android/server/wm/ProtoLogIntegrationTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/ProtoLogIntegrationTest.java @@ -16,6 +16,7 @@ package com.android.server.wm; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -47,7 +48,7 @@ public class ProtoLogIntegrationTest { ProtoLogGroup.testProtoLog(); verify(mockedProtoLog).log(eq(ProtoLogImpl.LogLevel.ERROR), eq( ProtoLogGroup.TEST_GROUP), - eq(485522692), eq(0b0010101001010111), + anyInt(), eq(0b0010101001010111), eq(ProtoLogGroup.TEST_GROUP.isLogToLogcat() ? "Test completed successfully: %b %d %o %x %e %g %f %% %s" : null), diff --git a/startop/view_compiler/dex_builder.cc b/startop/view_compiler/dex_builder.cc index 499c42e2888b..48b44d0fc99b 100644 --- a/startop/view_compiler/dex_builder.cc +++ b/startop/view_compiler/dex_builder.cc @@ -161,7 +161,7 @@ void WriteTestDexFile(const string& filename) { MethodBuilder method{cbuilder.CreateMethod("foo", Prototype{TypeDescriptor::Int(), string_type})}; - Value result = method.MakeRegister(); + LiveRegister result = method.AllocRegister(); MethodDeclData string_length = dex_file.GetOrDeclareMethod(string_type, "length", Prototype{TypeDescriptor::Int()}); @@ -314,7 +314,7 @@ ir::EncodedMethod* MethodBuilder::Encode() { CHECK(decl_->prototype != nullptr); size_t const num_args = decl_->prototype->param_types != nullptr ? decl_->prototype->param_types->types.size() : 0; - code->registers = num_registers_ + num_args + kMaxScratchRegisters; + code->registers = NumRegisters() + num_args + kMaxScratchRegisters; code->ins_count = num_args; EncodeInstructions(); code->instructions = slicer::ArrayView<const ::dex::u2>(buffer_.data(), buffer_.size()); @@ -327,7 +327,20 @@ ir::EncodedMethod* MethodBuilder::Encode() { return method; } -Value MethodBuilder::MakeRegister() { return Value::Local(num_registers_++); } +LiveRegister MethodBuilder::AllocRegister() { + // Find a free register + for (size_t i = 0; i < register_liveness_.size(); ++i) { + if (!register_liveness_[i]) { + register_liveness_[i] = true; + return LiveRegister{®ister_liveness_, i}; + } + } + + // If we get here, all the registers are in use, so we have to allocate a new + // one. + register_liveness_.push_back(true); + return LiveRegister{®ister_liveness_, register_liveness_.size() - 1}; +} Value MethodBuilder::MakeLabel() { labels_.push_back({}); @@ -600,7 +613,7 @@ size_t MethodBuilder::RegisterValue(const Value& value) const { if (value.is_register()) { return value.value(); } else if (value.is_parameter()) { - return value.value() + num_registers_ + kMaxScratchRegisters; + return value.value() + NumRegisters() + kMaxScratchRegisters; } CHECK(false && "Must be either a parameter or a register"); return 0; diff --git a/startop/view_compiler/dex_builder.h b/startop/view_compiler/dex_builder.h index 292d6599c115..3924e77fab59 100644 --- a/startop/view_compiler/dex_builder.h +++ b/startop/view_compiler/dex_builder.h @@ -140,6 +140,29 @@ class Value { constexpr Value(size_t value, Kind kind) : value_{value}, kind_{kind} {} }; +// Represents an allocated register returned by MethodBuilder::AllocRegister +class LiveRegister { + friend class MethodBuilder; + + public: + LiveRegister(LiveRegister&& other) : liveness_{other.liveness_}, index_{other.index_} { + other.index_ = {}; + }; + ~LiveRegister() { + if (index_.has_value()) { + (*liveness_)[*index_] = false; + } + }; + + operator const Value() const { return Value::Local(*index_); } + + private: + LiveRegister(std::vector<bool>* liveness, size_t index) : liveness_{liveness}, index_{index} {} + + std::vector<bool>* const liveness_; + std::optional<size_t> index_; +}; + // A virtual instruction. We convert these to real instructions in MethodBuilder::Encode. // Virtual instructions are needed to keep track of information that is not known until all of the // code is generated. This information includes things like how many local registers are created and @@ -178,7 +201,8 @@ class Instruction { } // For most instructions, which take some number of arguments and have an optional return value. template <typename... T> - static inline Instruction OpWithArgs(Op opcode, std::optional<const Value> dest, T... args) { + static inline Instruction OpWithArgs(Op opcode, std::optional<const Value> dest, + const T&... args) { return Instruction{opcode, /*index_argument=*/0, /*result_is_object=*/false, dest, args...}; } @@ -199,14 +223,14 @@ class Instruction { template <typename... T> static inline Instruction InvokeVirtualObject(size_t index_argument, std::optional<const Value> dest, Value this_arg, - T... args) { + const T&... args) { return Instruction{ Op::kInvokeVirtual, index_argument, /*result_is_object=*/true, dest, this_arg, args...}; } // For direct calls (basically, constructors). template <typename... T> static inline Instruction InvokeDirect(size_t index_argument, std::optional<const Value> dest, - Value this_arg, T... args) { + Value this_arg, const T&... args) { return Instruction{ Op::kInvokeDirect, index_argument, /*result_is_object=*/false, dest, this_arg, args...}; } @@ -234,7 +258,7 @@ class Instruction { // For static calls. template <typename... T> static inline Instruction InvokeInterface(size_t index_argument, std::optional<const Value> dest, - T... args) { + const T&... args) { return Instruction{ Op::kInvokeInterface, index_argument, /*result_is_object=*/false, dest, args...}; } @@ -277,7 +301,7 @@ class Instruction { template <typename... T> inline Instruction(Op opcode, size_t index_argument, bool result_is_object, - std::optional<const Value> dest, T... args) + std::optional<const Value> dest, const T&... args) : opcode_{opcode}, index_argument_{index_argument}, result_is_object_{result_is_object}, @@ -309,10 +333,8 @@ class MethodBuilder { // Encode the method into DEX format. ir::EncodedMethod* Encode(); - // Create a new register to be used to storing values. Note that these are not SSA registers, like - // might be expected in similar code generators. This does no liveness tracking or anything, so - // it's up to the caller to reuse registers as appropriate. - Value MakeRegister(); + // Create a new register to be used to storing values. + LiveRegister AllocRegister(); Value MakeLabel(); @@ -329,7 +351,7 @@ class MethodBuilder { void BuildConst4(Value target, int value); void BuildConstString(Value target, const std::string& value); template <typename... T> - void BuildNew(Value target, TypeDescriptor type, Prototype constructor, T... args); + void BuildNew(Value target, TypeDescriptor type, Prototype constructor, const T&... args); // TODO: add builders for more instructions @@ -427,7 +449,7 @@ class MethodBuilder { static_assert(num_regs <= kMaxScratchRegisters); std::array<Value, num_regs> regs; for (size_t i = 0; i < num_regs; ++i) { - regs[i] = std::move(Value::Local(num_registers_ + i)); + regs[i] = std::move(Value::Local(NumRegisters() + i)); } return regs; } @@ -457,8 +479,9 @@ class MethodBuilder { // around to make legal DEX code. static constexpr size_t kMaxScratchRegisters = 5; - // How many registers we've allocated - size_t num_registers_{0}; + size_t NumRegisters() const { + return register_liveness_.size(); + } // Stores information needed to back-patch a label once it is bound. We need to know the start of // the instruction that refers to the label, and the offset to where the actual label value should @@ -478,6 +501,8 @@ class MethodBuilder { // During encoding, keep track of the largest number of arguments needed, so we can use it for our // outs count size_t max_args_{0}; + + std::vector<bool> register_liveness_; }; // A helper to build class definitions. @@ -576,7 +601,8 @@ class DexBuilder { }; template <typename... T> -void MethodBuilder::BuildNew(Value target, TypeDescriptor type, Prototype constructor, T... args) { +void MethodBuilder::BuildNew(Value target, TypeDescriptor type, Prototype constructor, + const T&... args) { MethodDeclData constructor_data{dex_->GetOrDeclareMethod(type, "<init>", constructor)}; // allocate the object ir::Type* type_def = dex_->GetOrAddType(type.descriptor()); diff --git a/startop/view_compiler/dex_layout_compiler.cc b/startop/view_compiler/dex_layout_compiler.cc index 8febfb71ecd1..cb820f8f20fb 100644 --- a/startop/view_compiler/dex_layout_compiler.cc +++ b/startop/view_compiler/dex_layout_compiler.cc @@ -22,76 +22,94 @@ namespace startop { using android::base::StringPrintf; +using dex::Instruction; +using dex::LiveRegister; +using dex::Prototype; +using dex::TypeDescriptor; +using dex::Value; + +namespace { +// TODO: these are a bunch of static initializers, which we should avoid. See if +// we can make them constexpr. +const TypeDescriptor kAttributeSet = TypeDescriptor::FromClassname("android.util.AttributeSet"); +const TypeDescriptor kContext = TypeDescriptor::FromClassname("android.content.Context"); +const TypeDescriptor kLayoutInflater = TypeDescriptor::FromClassname("android.view.LayoutInflater"); +const TypeDescriptor kResources = TypeDescriptor::FromClassname("android.content.res.Resources"); +const TypeDescriptor kString = TypeDescriptor::FromClassname("java.lang.String"); +const TypeDescriptor kView = TypeDescriptor::FromClassname("android.view.View"); +const TypeDescriptor kViewGroup = TypeDescriptor::FromClassname("android.view.ViewGroup"); +const TypeDescriptor kXmlResourceParser = + TypeDescriptor::FromClassname("android.content.res.XmlResourceParser"); +} // namespace DexViewBuilder::DexViewBuilder(dex::MethodBuilder* method) : method_{method}, - context_{dex::Value::Parameter(0)}, - resid_{dex::Value::Parameter(1)}, - inflater_{method->MakeRegister()}, - xml_{method->MakeRegister()}, - attrs_{method->MakeRegister()}, - classname_tmp_{method->MakeRegister()}, - xml_next_{method->dex_file()->GetOrDeclareMethod( - dex::TypeDescriptor::FromClassname("android.content.res.XmlResourceParser"), "next", - dex::Prototype{dex::TypeDescriptor::Int()})}, + context_{Value::Parameter(0)}, + resid_{Value::Parameter(1)}, + inflater_{method->AllocRegister()}, + xml_{method->AllocRegister()}, + attrs_{method->AllocRegister()}, + classname_tmp_{method->AllocRegister()}, + xml_next_{method->dex_file()->GetOrDeclareMethod(kXmlResourceParser, "next", + Prototype{TypeDescriptor::Int()})}, try_create_view_{method->dex_file()->GetOrDeclareMethod( - dex::TypeDescriptor::FromClassname("android.view.LayoutInflater"), "tryCreateView", - dex::Prototype{dex::TypeDescriptor::FromClassname("android.view.View"), - dex::TypeDescriptor::FromClassname("android.view.View"), - dex::TypeDescriptor::FromClassname("java.lang.String"), - dex::TypeDescriptor::FromClassname("android.content.Context"), - dex::TypeDescriptor::FromClassname("android.util.AttributeSet")})}, + kLayoutInflater, "tryCreateView", + Prototype{kView, kView, kString, kContext, kAttributeSet})}, generate_layout_params_{method->dex_file()->GetOrDeclareMethod( - dex::TypeDescriptor::FromClassname("android.view.ViewGroup"), "generateLayoutParams", - dex::Prototype{dex::TypeDescriptor::FromClassname("android.view.ViewGroup$LayoutParams"), - dex::TypeDescriptor::FromClassname("android.util.AttributeSet")})}, + kViewGroup, "generateLayoutParams", + Prototype{TypeDescriptor::FromClassname("android.view.ViewGroup$LayoutParams"), + kAttributeSet})}, add_view_{method->dex_file()->GetOrDeclareMethod( - dex::TypeDescriptor::FromClassname("android.view.ViewGroup"), "addView", - dex::Prototype{ - dex::TypeDescriptor::Void(), - dex::TypeDescriptor::FromClassname("android.view.View"), - dex::TypeDescriptor::FromClassname("android.view.ViewGroup$LayoutParams")})}, - // The register stack starts with one register, which will be null for the root view. - register_stack_{{method->MakeRegister()}} {} - -void DexViewBuilder::Start() { - dex::DexBuilder* const dex = method_->dex_file(); - - // LayoutInflater inflater = LayoutInflater.from(context); - auto layout_inflater_from = dex->GetOrDeclareMethod( - dex::TypeDescriptor::FromClassname("android.view.LayoutInflater"), - "from", - dex::Prototype{dex::TypeDescriptor::FromClassname("android.view.LayoutInflater"), - dex::TypeDescriptor::FromClassname("android.content.Context")}); - method_->AddInstruction( - dex::Instruction::InvokeStaticObject(layout_inflater_from.id, /*dest=*/inflater_, context_)); + kViewGroup, "addView", + Prototype{TypeDescriptor::Void(), + kView, + TypeDescriptor::FromClassname("android.view.ViewGroup$LayoutParams")})} {} + +void DexViewBuilder::BuildGetLayoutInflater(Value dest) { + // dest = LayoutInflater.from(context); + auto layout_inflater_from = method_->dex_file()->GetOrDeclareMethod( + kLayoutInflater, "from", Prototype{kLayoutInflater, kContext}); + method_->AddInstruction(Instruction::InvokeStaticObject(layout_inflater_from.id, dest, context_)); +} - // Resources res = context.getResources(); - auto context_type = dex::TypeDescriptor::FromClassname("android.content.Context"); - auto resources_type = dex::TypeDescriptor::FromClassname("android.content.res.Resources"); +void DexViewBuilder::BuildGetResources(Value dest) { + // dest = context.getResources(); auto get_resources = - dex->GetOrDeclareMethod(context_type, "getResources", dex::Prototype{resources_type}); - method_->AddInstruction(dex::Instruction::InvokeVirtualObject(get_resources.id, xml_, context_)); - - // XmlResourceParser xml = res.getLayout(resid); - auto xml_resource_parser_type = - dex::TypeDescriptor::FromClassname("android.content.res.XmlResourceParser"); - auto get_layout = - dex->GetOrDeclareMethod(resources_type, - "getLayout", - dex::Prototype{xml_resource_parser_type, dex::TypeDescriptor::Int()}); - method_->AddInstruction(dex::Instruction::InvokeVirtualObject(get_layout.id, xml_, xml_, resid_)); - - // AttributeSet attrs = Xml.asAttributeSet(xml); - auto as_attribute_set = dex->GetOrDeclareMethod( - dex::TypeDescriptor::FromClassname("android.util.Xml"), + method_->dex_file()->GetOrDeclareMethod(kContext, "getResources", Prototype{kResources}); + method_->AddInstruction(Instruction::InvokeVirtualObject(get_resources.id, dest, context_)); +} + +void DexViewBuilder::BuildGetLayoutResource(Value dest, Value resources, Value resid) { + // dest = resources.getLayout(resid); + auto get_layout = method_->dex_file()->GetOrDeclareMethod( + kResources, "getLayout", Prototype{kXmlResourceParser, TypeDescriptor::Int()}); + method_->AddInstruction(Instruction::InvokeVirtualObject(get_layout.id, dest, resources, resid)); +} + +void DexViewBuilder::BuildLayoutResourceToAttributeSet(dex::Value dest, + dex::Value layout_resource) { + // dest = Xml.asAttributeSet(layout_resource); + auto as_attribute_set = method_->dex_file()->GetOrDeclareMethod( + TypeDescriptor::FromClassname("android.util.Xml"), "asAttributeSet", - dex::Prototype{dex::TypeDescriptor::FromClassname("android.util.AttributeSet"), - dex::TypeDescriptor::FromClassname("org.xmlpull.v1.XmlPullParser")}); - method_->AddInstruction(dex::Instruction::InvokeStaticObject(as_attribute_set.id, attrs_, xml_)); + Prototype{kAttributeSet, TypeDescriptor::FromClassname("org.xmlpull.v1.XmlPullParser")}); + method_->AddInstruction( + Instruction::InvokeStaticObject(as_attribute_set.id, dest, layout_resource)); +} - // xml.next(); // start document - method_->AddInstruction(dex::Instruction::InvokeInterface(xml_next_.id, {}, xml_)); +void DexViewBuilder::BuildXmlNext() { + // xml_.next(); + method_->AddInstruction(Instruction::InvokeInterface(xml_next_.id, {}, xml_)); +} + +void DexViewBuilder::Start() { + BuildGetLayoutInflater(/*dest=*/inflater_); + BuildGetResources(/*dest=*/xml_); + BuildGetLayoutResource(/*dest=*/xml_, /*resources=*/xml_, resid_); + BuildLayoutResourceToAttributeSet(/*dest=*/attrs_, /*layout_resource=*/xml_); + + // Advance past start document tag + BuildXmlNext(); } void DexViewBuilder::Finish() {} @@ -107,58 +125,57 @@ std::string ResolveName(const std::string& name) { } } // namespace +void DexViewBuilder::BuildTryCreateView(Value dest, Value parent, Value classname) { + // dest = inflater_.tryCreateView(parent, classname, context_, attrs_); + method_->AddInstruction(Instruction::InvokeVirtualObject( + try_create_view_.id, dest, inflater_, parent, classname, context_, attrs_)); +} + void DexViewBuilder::StartView(const std::string& name, bool is_viewgroup) { bool const is_root_view = view_stack_.empty(); - // xml.next(); // start tag - method_->AddInstruction(dex::Instruction::InvokeInterface(xml_next_.id, {}, xml_)); + // Advance to start tag + BuildXmlNext(); - dex::Value view = AcquireRegister(); + LiveRegister view = AcquireRegister(); // try to create the view using the factories method_->BuildConstString(classname_tmp_, name); // TODO: the need to fully qualify the classname if (is_root_view) { - dex::Value null = AcquireRegister(); + LiveRegister null = AcquireRegister(); method_->BuildConst4(null, 0); - method_->AddInstruction(dex::Instruction::InvokeVirtualObject( - try_create_view_.id, view, inflater_, null, classname_tmp_, context_, attrs_)); - ReleaseRegister(); + BuildTryCreateView(/*dest=*/view, /*parent=*/null, classname_tmp_); } else { - method_->AddInstruction(dex::Instruction::InvokeVirtualObject( - try_create_view_.id, view, inflater_, GetCurrentView(), classname_tmp_, context_, attrs_)); + BuildTryCreateView(/*dest=*/view, /*parent=*/GetCurrentView(), classname_tmp_); } auto label = method_->MakeLabel(); // branch if not null method_->AddInstruction( - dex::Instruction::OpWithArgs(dex::Instruction::Op::kBranchNEqz, /*dest=*/{}, view, label)); + Instruction::OpWithArgs(Instruction::Op::kBranchNEqz, /*dest=*/{}, view, label)); // If null, create the class directly. method_->BuildNew(view, - dex::TypeDescriptor::FromClassname(ResolveName(name)), - dex::Prototype{dex::TypeDescriptor::Void(), - dex::TypeDescriptor::FromClassname("android.content.Context"), - dex::TypeDescriptor::FromClassname("android.util.AttributeSet")}, + TypeDescriptor::FromClassname(ResolveName(name)), + Prototype{TypeDescriptor::Void(), kContext, kAttributeSet}, context_, attrs_); - method_->AddInstruction( - dex::Instruction::OpWithArgs(dex::Instruction::Op::kBindLabel, /*dest=*/{}, label)); + method_->AddInstruction(Instruction::OpWithArgs(Instruction::Op::kBindLabel, /*dest=*/{}, label)); if (is_viewgroup) { // Cast to a ViewGroup so we can add children later. - const ir::Type* view_group_def = method_->dex_file()->GetOrAddType( - dex::TypeDescriptor::FromClassname("android.view.ViewGroup").descriptor()); - method_->AddInstruction(dex::Instruction::Cast(view, dex::Value::Type(view_group_def->orig_index))); + const ir::Type* view_group_def = method_->dex_file()->GetOrAddType(kViewGroup.descriptor()); + method_->AddInstruction(Instruction::Cast(view, Value::Type(view_group_def->orig_index))); } if (!is_root_view) { // layout_params = parent.generateLayoutParams(attrs); - dex::Value layout_params{AcquireRegister()}; - method_->AddInstruction(dex::Instruction::InvokeVirtualObject( + LiveRegister layout_params{AcquireRegister()}; + method_->AddInstruction(Instruction::InvokeVirtualObject( generate_layout_params_.id, layout_params, GetCurrentView(), attrs_)); - view_stack_.push_back({view, layout_params}); + view_stack_.push_back({std::move(view), std::move(layout_params)}); } else { - view_stack_.push_back({view, {}}); + view_stack_.push_back({std::move(view), {}}); } } @@ -167,40 +184,24 @@ void DexViewBuilder::FinishView() { method_->BuildReturn(GetCurrentView(), /*is_object=*/true); } else { // parent.add(view, layout_params) - method_->AddInstruction(dex::Instruction::InvokeVirtual( + method_->AddInstruction(Instruction::InvokeVirtual( add_view_.id, /*dest=*/{}, GetParentView(), GetCurrentView(), GetCurrentLayoutParams())); // xml.next(); // end tag - method_->AddInstruction(dex::Instruction::InvokeInterface(xml_next_.id, {}, xml_)); + method_->AddInstruction(Instruction::InvokeInterface(xml_next_.id, {}, xml_)); } PopViewStack(); } -dex::Value DexViewBuilder::AcquireRegister() { - top_register_++; - if (register_stack_.size() == top_register_) { - register_stack_.push_back(method_->MakeRegister()); - } - return register_stack_[top_register_]; -} - -void DexViewBuilder::ReleaseRegister() { top_register_--; } +LiveRegister DexViewBuilder::AcquireRegister() { return method_->AllocRegister(); } -dex::Value DexViewBuilder::GetCurrentView() const { return view_stack_.back().view; } -dex::Value DexViewBuilder::GetCurrentLayoutParams() const { +Value DexViewBuilder::GetCurrentView() const { return view_stack_.back().view; } +Value DexViewBuilder::GetCurrentLayoutParams() const { return view_stack_.back().layout_params.value(); } -dex::Value DexViewBuilder::GetParentView() const { - return view_stack_[view_stack_.size() - 2].view; -} +Value DexViewBuilder::GetParentView() const { return view_stack_[view_stack_.size() - 2].view; } void DexViewBuilder::PopViewStack() { - const auto& top = view_stack_.back(); - // release the layout params if we have them - if (top.layout_params.has_value()) { - ReleaseRegister(); - } // Unconditionally release the view register. - ReleaseRegister(); view_stack_.pop_back(); } diff --git a/startop/view_compiler/dex_layout_compiler.h b/startop/view_compiler/dex_layout_compiler.h index 170a1a610297..a34ed1f0168e 100644 --- a/startop/view_compiler/dex_layout_compiler.h +++ b/startop/view_compiler/dex_layout_compiler.h @@ -79,36 +79,41 @@ class DexViewBuilder { private: // Accessors for the stack of views that are under construction. - dex::Value AcquireRegister(); - void ReleaseRegister(); + dex::LiveRegister AcquireRegister(); dex::Value GetCurrentView() const; dex::Value GetCurrentLayoutParams() const; dex::Value GetParentView() const; void PopViewStack(); + // Methods to simplify building different code fragments. + void BuildGetLayoutInflater(dex::Value dest); + void BuildGetResources(dex::Value dest); + void BuildGetLayoutResource(dex::Value dest, dex::Value resources, dex::Value resid); + void BuildLayoutResourceToAttributeSet(dex::Value dest, dex::Value layout_resource); + void BuildXmlNext(); + void BuildTryCreateView(dex::Value dest, dex::Value parent, dex::Value classname); + dex::MethodBuilder* method_; - // Registers used for code generation + // Parameters to the generated method dex::Value const context_; dex::Value const resid_; - const dex::Value inflater_; - const dex::Value xml_; - const dex::Value attrs_; - const dex::Value classname_tmp_; + + // Registers used for code generation + const dex::LiveRegister inflater_; + const dex::LiveRegister xml_; + const dex::LiveRegister attrs_; + const dex::LiveRegister classname_tmp_; const dex::MethodDeclData xml_next_; const dex::MethodDeclData try_create_view_; const dex::MethodDeclData generate_layout_params_; const dex::MethodDeclData add_view_; - // used for keeping track of which registers are in use - size_t top_register_{0}; - std::vector<dex::Value> register_stack_; - // Keep track of the views currently in progress. struct ViewEntry { - dex::Value view; - std::optional<dex::Value> layout_params; + dex::LiveRegister view; + std::optional<dex::LiveRegister> layout_params; }; std::vector<ViewEntry> view_stack_; }; diff --git a/startop/view_compiler/dex_testcase_generator.cc b/startop/view_compiler/dex_testcase_generator.cc index 6dedf24e290d..5dda59e3473f 100644 --- a/startop/view_compiler/dex_testcase_generator.cc +++ b/startop/view_compiler/dex_testcase_generator.cc @@ -47,7 +47,7 @@ void GenerateSimpleTestCases(const string& outdir) { // int return5() { return 5; } auto return5{cbuilder.CreateMethod("return5", Prototype{TypeDescriptor::Int()})}; { - Value r{return5.MakeRegister()}; + LiveRegister r{return5.AllocRegister()}; return5.BuildConst4(r, 5); return5.BuildReturn(r); } @@ -57,9 +57,9 @@ void GenerateSimpleTestCases(const string& outdir) { auto integer_type{TypeDescriptor::FromClassname("java.lang.Integer")}; auto returnInteger5{cbuilder.CreateMethod("returnInteger5", Prototype{integer_type})}; [&](MethodBuilder& method) { - Value five{method.MakeRegister()}; + LiveRegister five{method.AllocRegister()}; method.BuildConst4(five, 5); - Value object{method.MakeRegister()}; + LiveRegister object{method.AllocRegister()}; method.BuildNew( object, integer_type, Prototype{TypeDescriptor::Void(), TypeDescriptor::Int()}, five); method.BuildReturn(object, /*is_object=*/true); @@ -80,7 +80,7 @@ void GenerateSimpleTestCases(const string& outdir) { auto returnStringLength{ cbuilder.CreateMethod("returnStringLength", Prototype{TypeDescriptor::Int(), string_type})}; { - Value result = returnStringLength.MakeRegister(); + LiveRegister result = returnStringLength.AllocRegister(); returnStringLength.AddInstruction( Instruction::InvokeVirtual(string_length.id, result, Value::Parameter(0))); returnStringLength.BuildReturn(result); @@ -91,7 +91,7 @@ void GenerateSimpleTestCases(const string& outdir) { MethodBuilder returnIfZero{cbuilder.CreateMethod( "returnIfZero", Prototype{TypeDescriptor::Int(), TypeDescriptor::Int()})}; { - Value resultIfZero{returnIfZero.MakeRegister()}; + LiveRegister resultIfZero{returnIfZero.AllocRegister()}; Value else_target{returnIfZero.MakeLabel()}; returnIfZero.AddInstruction(Instruction::OpWithArgs( Instruction::Op::kBranchEqz, /*dest=*/{}, Value::Parameter(0), else_target)); @@ -112,7 +112,7 @@ void GenerateSimpleTestCases(const string& outdir) { MethodBuilder returnIfNotZero{cbuilder.CreateMethod( "returnIfNotZero", Prototype{TypeDescriptor::Int(), TypeDescriptor::Int()})}; { - Value resultIfNotZero{returnIfNotZero.MakeRegister()}; + LiveRegister resultIfNotZero{returnIfNotZero.AllocRegister()}; Value else_target{returnIfNotZero.MakeLabel()}; returnIfNotZero.AddInstruction(Instruction::OpWithArgs( Instruction::Op::kBranchNEqz, /*dest=*/{}, Value::Parameter(0), else_target)); @@ -148,8 +148,8 @@ void GenerateSimpleTestCases(const string& outdir) { MethodBuilder backwardsBranch{ cbuilder.CreateMethod("backwardsBranch", Prototype{TypeDescriptor::Int()})}; [](MethodBuilder& method) { - Value zero = method.MakeRegister(); - Value result = method.MakeRegister(); + LiveRegister zero = method.AllocRegister(); + LiveRegister result = method.AllocRegister(); Value labelA = method.MakeLabel(); Value labelB = method.MakeLabel(); method.BuildConst4(zero, 0); @@ -177,7 +177,7 @@ void GenerateSimpleTestCases(const string& outdir) { // public static String returnNull() { return null; } MethodBuilder returnNull{cbuilder.CreateMethod("returnNull", Prototype{string_type})}; [](MethodBuilder& method) { - Value zero = method.MakeRegister(); + LiveRegister zero = method.AllocRegister(); method.BuildConst4(zero, 0); method.BuildReturn(zero, /*is_object=*/true); }(returnNull); @@ -188,7 +188,7 @@ void GenerateSimpleTestCases(const string& outdir) { // public static String makeString() { return "Hello, World!"; } MethodBuilder makeString{cbuilder.CreateMethod("makeString", Prototype{string_type})}; [](MethodBuilder& method) { - Value string = method.MakeRegister(); + LiveRegister string = method.AllocRegister(); method.BuildConstString(string, "Hello, World!"); method.BuildReturn(string, /*is_object=*/true); }(makeString); @@ -200,7 +200,7 @@ void GenerateSimpleTestCases(const string& outdir) { MethodBuilder returnStringIfZeroAB{ cbuilder.CreateMethod("returnStringIfZeroAB", Prototype{string_type, TypeDescriptor::Int()})}; [&](MethodBuilder& method) { - Value resultIfZero{method.MakeRegister()}; + LiveRegister resultIfZero{method.AllocRegister()}; Value else_target{method.MakeLabel()}; method.AddInstruction(Instruction::OpWithArgs( Instruction::Op::kBranchEqz, /*dest=*/{}, Value::Parameter(0), else_target)); @@ -220,7 +220,7 @@ void GenerateSimpleTestCases(const string& outdir) { MethodBuilder returnStringIfZeroBA{ cbuilder.CreateMethod("returnStringIfZeroBA", Prototype{string_type, TypeDescriptor::Int()})}; [&](MethodBuilder& method) { - Value resultIfZero{method.MakeRegister()}; + LiveRegister resultIfZero{method.AllocRegister()}; Value else_target{method.MakeLabel()}; method.AddInstruction(Instruction::OpWithArgs( Instruction::Op::kBranchEqz, /*dest=*/{}, Value::Parameter(0), else_target)); @@ -244,7 +244,7 @@ void GenerateSimpleTestCases(const string& outdir) { cbuilder.CreateMethod("invokeStaticReturnObject", Prototype{string_type, TypeDescriptor::Int(), TypeDescriptor::Int()})}; [&](MethodBuilder& method) { - Value result{method.MakeRegister()}; + LiveRegister result{method.AllocRegister()}; MethodDeclData to_string{dex_file.GetOrDeclareMethod( TypeDescriptor::FromClassname("java.lang.Integer"), "toString", @@ -260,7 +260,7 @@ void GenerateSimpleTestCases(const string& outdir) { MethodBuilder invokeVirtualReturnObject{cbuilder.CreateMethod( "invokeVirtualReturnObject", Prototype{string_type, string_type, TypeDescriptor::Int()})}; [&](MethodBuilder& method) { - Value result{method.MakeRegister()}; + LiveRegister result{method.AllocRegister()}; MethodDeclData substring{dex_file.GetOrDeclareMethod( string_type, "substring", Prototype{string_type, TypeDescriptor::Int()})}; method.AddInstruction(Instruction::InvokeVirtualObject( @@ -291,7 +291,7 @@ void GenerateSimpleTestCases(const string& outdir) { [&](MethodBuilder& method) { const ir::FieldDecl* field = dex_file.GetOrAddField(test_class, "staticInteger", TypeDescriptor::Int()); - Value result{method.MakeRegister()}; + LiveRegister result{method.AllocRegister()}; method.AddInstruction(Instruction::GetStaticField(field->orig_index, result)); method.BuildReturn(result, /*is_object=*/false); method.Encode(); @@ -304,7 +304,7 @@ void GenerateSimpleTestCases(const string& outdir) { [&](MethodBuilder& method) { const ir::FieldDecl* field = dex_file.GetOrAddField(test_class, "staticInteger", TypeDescriptor::Int()); - Value number{method.MakeRegister()}; + LiveRegister number{method.AllocRegister()}; method.BuildConst4(number, 7); method.AddInstruction(Instruction::SetStaticField(field->orig_index, number)); method.BuildReturn(); @@ -318,7 +318,7 @@ void GenerateSimpleTestCases(const string& outdir) { [&](MethodBuilder& method) { const ir::FieldDecl* field = dex_file.GetOrAddField(test_class, "instanceField", TypeDescriptor::Int()); - Value result{method.MakeRegister()}; + LiveRegister result{method.AllocRegister()}; method.AddInstruction(Instruction::GetField(field->orig_index, result, Value::Parameter(0))); method.BuildReturn(result, /*is_object=*/false); method.Encode(); @@ -331,7 +331,7 @@ void GenerateSimpleTestCases(const string& outdir) { [&](MethodBuilder& method) { const ir::FieldDecl* field = dex_file.GetOrAddField(test_class, "instanceField", TypeDescriptor::Int()); - Value number{method.MakeRegister()}; + LiveRegister number{method.AllocRegister()}; method.BuildConst4(number, 7); method.AddInstruction(Instruction::SetField(field->orig_index, Value::Parameter(0), number)); method.BuildReturn(); diff --git a/telephony/java/com/android/internal/telephony/gsm/SmsMessage.java b/telephony/java/com/android/internal/telephony/gsm/SmsMessage.java index 28b331b17b91..da32c8c45a73 100644 --- a/telephony/java/com/android/internal/telephony/gsm/SmsMessage.java +++ b/telephony/java/com/android/internal/telephony/gsm/SmsMessage.java @@ -1496,7 +1496,7 @@ public class SmsMessage extends SmsMessageBase { * * @return true if this is a USIM data download message; false otherwise */ - public boolean isUsimDataDownload() { + boolean isUsimDataDownload() { return messageClass == MessageClass.CLASS_2 && (mProtocolIdentifier == 0x7f || mProtocolIdentifier == 0x7c); } diff --git a/tests/ApkVerityTest/Android.bp b/tests/ApkVerityTest/Android.bp new file mode 100644 index 000000000000..adcbb4287dd0 --- /dev/null +++ b/tests/ApkVerityTest/Android.bp @@ -0,0 +1,34 @@ +// Copyright (C) 2019 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. + +java_test_host { + name: "ApkVerityTests", + srcs: ["src/**/*.java"], + libs: ["tradefed", "compatibility-tradefed", "compatibility-host-util"], + test_suites: ["general-tests"], + target_required: [ + "block_device_writer_module", + "ApkVerityTestApp", + "ApkVerityTestAppSplit", + ], + data: [ + ":ApkVerityTestCertDer", + ":ApkVerityTestAppFsvSig", + ":ApkVerityTestAppDm", + ":ApkVerityTestAppDmFsvSig", + ":ApkVerityTestAppSplitFsvSig", + ":ApkVerityTestAppSplitDm", + ":ApkVerityTestAppSplitDmFsvSig", + ], +} diff --git a/tests/ApkVerityTest/AndroidTest.xml b/tests/ApkVerityTest/AndroidTest.xml new file mode 100644 index 000000000000..73779cbd1a87 --- /dev/null +++ b/tests/ApkVerityTest/AndroidTest.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2019 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. +--> +<configuration description="APK fs-verity integration/regression test"> + <option name="test-suite-tag" value="apct" /> + + <!-- This test requires root to write against block device. --> + <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer" /> + + <target_preparer class="com.android.tradefed.targetprep.DeviceSetup"> + <!-- Disable package verifier prevents it holding the target APK's fd that prevents cache + eviction. --> + <option name="set-global-setting" key="package_verifier_enable" value="0" /> + <option name="restore-settings" value="true" /> + + <!-- Skip in order to prevent reboot that confuses the test flow. --> + <option name="force-skip-system-props" value="true" /> + </target_preparer> + + <target_preparer class="com.android.tradefed.targetprep.PushFilePreparer"> + <option name="cleanup" value="true" /> + <option name="push" value="block_device_writer->/data/local/tmp/block_device_writer" /> + <option name="push" value="ApkVerityTestCert.der->/data/local/tmp/ApkVerityTestCert.der" /> + </target_preparer> + + <test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" > + <option name="jar" value="ApkVerityTests.jar" /> + </test> +</configuration> diff --git a/tests/ApkVerityTest/ApkVerityTestApp/Android.bp b/tests/ApkVerityTest/ApkVerityTestApp/Android.bp new file mode 100644 index 000000000000..69632b215822 --- /dev/null +++ b/tests/ApkVerityTest/ApkVerityTestApp/Android.bp @@ -0,0 +1,29 @@ +// Copyright (C) 2019 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. + +android_test_helper_app { + name: "ApkVerityTestApp", + manifest: "AndroidManifest.xml", + srcs: ["src/**/*.java"], +} + +android_test_helper_app { + name: "ApkVerityTestAppSplit", + manifest: "feature_split/AndroidManifest.xml", + srcs: ["src/**/*.java"], + aaptflags: [ + "--custom-package com.android.apkverity.feature_x", + "--package-id 0x80", + ], +} diff --git a/tests/ApkVerityTest/ApkVerityTestApp/AndroidManifest.xml b/tests/ApkVerityTest/ApkVerityTestApp/AndroidManifest.xml new file mode 100644 index 000000000000..0b3ff77c2cdf --- /dev/null +++ b/tests/ApkVerityTest/ApkVerityTestApp/AndroidManifest.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + * Copyright (C) 2019 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. + --> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.apkverity"> + <application> + <activity android:name=".DummyActivity"/> + </application> +</manifest> diff --git a/tests/ApkVerityTest/ApkVerityTestApp/feature_split/AndroidManifest.xml b/tests/ApkVerityTest/ApkVerityTestApp/feature_split/AndroidManifest.xml new file mode 100644 index 000000000000..3f1a4f3a26a1 --- /dev/null +++ b/tests/ApkVerityTest/ApkVerityTestApp/feature_split/AndroidManifest.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + * Copyright (C) 2019 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. + --> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.apkverity" + android:isFeatureSplit="true" + split="feature_x"> + <application> + <activity android:name=".feature_x.DummyActivity"/> + </application> +</manifest> diff --git a/tests/ApkVerityTest/ApkVerityTestApp/feature_split/src/com/android/apkverity/feature_x/DummyActivity.java b/tests/ApkVerityTest/ApkVerityTestApp/feature_split/src/com/android/apkverity/feature_x/DummyActivity.java new file mode 100644 index 000000000000..0f694c293330 --- /dev/null +++ b/tests/ApkVerityTest/ApkVerityTestApp/feature_split/src/com/android/apkverity/feature_x/DummyActivity.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2019 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.apkverity.feature_x; + +import android.app.Activity; + +/** Dummy class just to generate some dex */ +public class DummyActivity extends Activity {} diff --git a/tests/ApkVerityTest/ApkVerityTestApp/src/com/android/apkverity/DummyActivity.java b/tests/ApkVerityTest/ApkVerityTestApp/src/com/android/apkverity/DummyActivity.java new file mode 100644 index 000000000000..837c7be37504 --- /dev/null +++ b/tests/ApkVerityTest/ApkVerityTestApp/src/com/android/apkverity/DummyActivity.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2019 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.apkverity; + +import android.app.Activity; + +/** Dummy class just to generate some dex */ +public class DummyActivity extends Activity {} diff --git a/tests/ApkVerityTest/block_device_writer/Android.bp b/tests/ApkVerityTest/block_device_writer/Android.bp new file mode 100644 index 000000000000..deed3a00d2fe --- /dev/null +++ b/tests/ApkVerityTest/block_device_writer/Android.bp @@ -0,0 +1,30 @@ +// Copyright (C) 2019 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. + +// This is a cc_test just because it supports test_suites. This should be converted to something +// like cc_binary_test_helper once supported. +cc_test { + // Depending on how the test runs, the executable may be uploaded to different location. + // Before the bug in the file pusher is fixed, workaround by making the name unique. + // See b/124718249#comment12. + name: "block_device_writer_module", + stem: "block_device_writer", + + srcs: ["block_device_writer.cpp"], + cflags: ["-Wall", "-Werror", "-Wextra", "-g"], + shared_libs: ["libbase", "libutils"], + + test_suites: ["general-tests"], + gtest: false, +} diff --git a/tests/ApkVerityTest/block_device_writer/block_device_writer.cpp b/tests/ApkVerityTest/block_device_writer/block_device_writer.cpp new file mode 100644 index 000000000000..b0c7251e77f5 --- /dev/null +++ b/tests/ApkVerityTest/block_device_writer/block_device_writer.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (C) 2019 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. + */ +#include <cassert> +#include <cstdio> +#include <cstdlib> +#include <cstring> +#include <memory> + +#include <errno.h> +#include <fcntl.h> +#include <linux/fiemap.h> +#include <linux/fs.h> +#include <sys/ioctl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> + +#include <android-base/unique_fd.h> + +// This program modifies a file at given offset, but directly against the block +// device, purposely to bypass the filesystem. Note that the change on block +// device may not reflect the same way when read from filesystem, for example, +// when the file is encrypted on disk. +// +// Only one byte is supported for now just so that we don't need to handle the +// case when the range crosses different "extents". +// +// References: +// https://www.kernel.org/doc/Documentation/filesystems/fiemap.txt +// https://git.kernel.org/pub/scm/fs/xfs/xfsprogs-dev.git/tree/io/fiemap.c + +ssize_t get_logical_block_size(const char* block_device) { + android::base::unique_fd fd(open(block_device, O_RDONLY)); + if (fd.get() < 0) { + fprintf(stderr, "open %s failed\n", block_device); + return -1; + } + + int size; + if (ioctl(fd, BLKSSZGET, &size) < 0) { + fprintf(stderr, "ioctl(BLKSSZGET) failed: %s\n", strerror(errno)); + return -1; + } + return size; +} + +int64_t get_physical_offset(const char* file_name, uint64_t byte_offset) { + android::base::unique_fd fd(open(file_name, O_RDONLY)); + if (fd.get() < 0) { + fprintf(stderr, "open %s failed\n", file_name); + return -1; + } + + const int map_size = sizeof(struct fiemap) + sizeof(struct fiemap_extent); + char fiemap_buffer[map_size] = {0}; + struct fiemap* fiemap = reinterpret_cast<struct fiemap*>(&fiemap_buffer); + + fiemap->fm_flags = FIEMAP_FLAG_SYNC; + fiemap->fm_start = byte_offset; + fiemap->fm_length = 1; + fiemap->fm_extent_count = 1; + + int ret = ioctl(fd.get(), FS_IOC_FIEMAP, fiemap); + if (ret < 0) { + fprintf(stderr, "ioctl(FS_IOC_FIEMAP) failed: %s\n", strerror(errno)); + return -1; + } + + if (fiemap->fm_mapped_extents != 1) { + fprintf(stderr, "fm_mapped_extents != 1 (is %d)\n", + fiemap->fm_mapped_extents); + return -1; + } + + struct fiemap_extent* extent = &fiemap->fm_extents[0]; + printf( + "logical offset: %llu, physical offset: %llu, length: %llu, " + "flags: %x\n", + extent->fe_logical, extent->fe_physical, extent->fe_length, + extent->fe_flags); + if (extent->fe_flags & (FIEMAP_EXTENT_UNKNOWN | + FIEMAP_EXTENT_UNWRITTEN)) { + fprintf(stderr, "Failed to locate physical offset safely\n"); + return -1; + } + + return extent->fe_physical + (byte_offset - extent->fe_logical); +} + +int read_block_from_device(const char* device_path, uint64_t block_offset, + ssize_t block_size, char* block_buffer) { + assert(block_offset % block_size == 0); + android::base::unique_fd fd(open(device_path, O_RDONLY | O_DIRECT)); + if (fd.get() < 0) { + fprintf(stderr, "open %s failed\n", device_path); + return -1; + } + + ssize_t retval = + TEMP_FAILURE_RETRY(pread(fd, block_buffer, block_size, block_offset)); + if (retval != block_size) { + fprintf(stderr, "read returns error or incomplete result (%zu): %s\n", + retval, strerror(errno)); + return -1; + } + return 0; +} + +int write_block_to_device(const char* device_path, uint64_t block_offset, + ssize_t block_size, char* block_buffer) { + assert(block_offset % block_size == 0); + android::base::unique_fd fd(open(device_path, O_WRONLY | O_DIRECT)); + if (fd.get() < 0) { + fprintf(stderr, "open %s failed\n", device_path); + return -1; + } + + ssize_t retval = TEMP_FAILURE_RETRY( + pwrite(fd.get(), block_buffer, block_size, block_offset)); + if (retval != block_size) { + fprintf(stderr, "write returns error or incomplete result (%zu): %s\n", + retval, strerror(errno)); + return -1; + } + return 0; +} + +int main(int argc, const char** argv) { + if (argc != 4) { + fprintf(stderr, + "Usage: %s block_dev filename byte_offset\n" + "\n" + "This program bypasses filesystem and damages the specified byte\n" + "at the physical position on <block_dev> corresponding to the\n" + "logical byte location in <filename>.\n", + argv[0]); + return -1; + } + + const char* block_device = argv[1]; + const char* file_name = argv[2]; + uint64_t byte_offset = strtoull(argv[3], nullptr, 10); + + ssize_t block_size = get_logical_block_size(block_device); + if (block_size < 0) { + return -1; + } + + int64_t physical_offset_signed = get_physical_offset(file_name, byte_offset); + if (physical_offset_signed < 0) { + return -1; + } + + uint64_t physical_offset = static_cast<uint64_t>(physical_offset_signed); + uint64_t offset_within_block = physical_offset % block_size; + uint64_t physical_block_offset = physical_offset - offset_within_block; + + // Direct I/O requires aligned buffer + std::unique_ptr<char> buf(static_cast<char*>( + aligned_alloc(block_size /* alignment */, block_size /* size */))); + + if (read_block_from_device(block_device, physical_block_offset, block_size, + buf.get()) < 0) { + return -1; + } + char* p = buf.get() + offset_within_block; + printf("before: %hhx\n", *p); + *p ^= 0xff; + printf("after: %hhx\n", *p); + if (write_block_to_device(block_device, physical_block_offset, block_size, + buf.get()) < 0) { + return -1; + } + + return 0; +} diff --git a/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java b/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java new file mode 100644 index 000000000000..761c5ceb2413 --- /dev/null +++ b/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java @@ -0,0 +1,496 @@ +/* + * Copyright (C) 2019 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.apkverity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeTrue; + +import android.platform.test.annotations.RootPermissionTest; + +import com.android.tradefed.device.DeviceNotAvailableException; +import com.android.tradefed.device.ITestDevice; +import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; +import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; +import com.android.tradefed.util.CommandResult; +import com.android.tradefed.util.CommandStatus; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.FileNotFoundException; +import java.util.Arrays; +import java.util.HashSet; + +/** + * This test makes sure app installs with fs-verity signature, and on-access verification works. + * + * <p>When an app is installed, all or none of the files should have their corresponding .fsv_sig + * signature file. Otherwise, install will fail. + * + * <p>Once installed, file protected by fs-verity is verified by kernel every time a block is loaded + * from disk to memory. The file is immutable by design, enforced by filesystem. + * + * <p>In order to make sure a block of the file is readable only if the underlying block on disk + * stay intact, the test needs to bypass the filesystem and tampers with the corresponding physical + * address against the block device. + * + * <p>Requirements to run this test: + * <ul> + * <li>Device is rootable</li> + * <li>The filesystem supports fs-verity</li> + * <li>The feature flag is enabled</li> + * </ul> + */ +@RootPermissionTest +@RunWith(DeviceJUnit4ClassRunner.class) +public class ApkVerityTest extends BaseHostJUnit4Test { + private static final String TARGET_PACKAGE = "com.android.apkverity"; + + private static final String BASE_APK = "ApkVerityTestApp.apk"; + private static final String BASE_APK_DM = "ApkVerityTestApp.dm"; + private static final String SPLIT_APK = "ApkVerityTestAppSplit.apk"; + private static final String SPLIT_APK_DM = "ApkVerityTestAppSplit.dm"; + + private static final String INSTALLED_BASE_APK = "base.apk"; + private static final String INSTALLED_BASE_DM = "base.dm"; + private static final String INSTALLED_SPLIT_APK = "split_feature_x.apk"; + private static final String INSTALLED_SPLIT_DM = "split_feature_x.dm"; + private static final String INSTALLED_BASE_APK_FSV_SIG = "base.apk.fsv_sig"; + private static final String INSTALLED_BASE_DM_FSV_SIG = "base.dm.fsv_sig"; + private static final String INSTALLED_SPLIT_APK_FSV_SIG = "split_feature_x.apk.fsv_sig"; + private static final String INSTALLED_SPLIT_DM_FSV_SIG = "split_feature_x.dm.fsv_sig"; + + private static final String DAMAGING_EXECUTABLE = "/data/local/tmp/block_device_writer"; + private static final String CERT_PATH = "/data/local/tmp/ApkVerityTestCert.der"; + + private static final String APK_VERITY_STANDARD_MODE = "2"; + + /** Only 4K page is supported by fs-verity currently. */ + private static final int FSVERITY_PAGE_SIZE = 4096; + + private ITestDevice mDevice; + private String mKeyId; + + @Before + public void setUp() throws DeviceNotAvailableException { + mDevice = getDevice(); + + String apkVerityMode = mDevice.getProperty("ro.apk_verity.mode"); + assumeTrue(APK_VERITY_STANDARD_MODE.equals(apkVerityMode)); + + mKeyId = expectRemoteCommandToSucceed( + "mini-keyctl padd asymmetric fsv_test .fs-verity < " + CERT_PATH).trim(); + if (!mKeyId.matches("^\\d+$")) { + String keyId = mKeyId; + mKeyId = null; + fail("Key ID is not decimal: " + keyId); + } + + uninstallPackage(TARGET_PACKAGE); + } + + @After + public void tearDown() throws DeviceNotAvailableException { + uninstallPackage(TARGET_PACKAGE); + + if (mKeyId != null) { + expectRemoteCommandToSucceed("mini-keyctl unlink " + mKeyId + " .fs-verity"); + } + } + + @Test + public void testFsverityKernelSupports() throws DeviceNotAvailableException { + ITestDevice.MountPointInfo mountPoint = mDevice.getMountPointInfo("/data"); + expectRemoteCommandToSucceed("test -f /sys/fs/" + mountPoint.type + "/features/verity"); + } + + @Test + public void testInstallBase() throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFileAndSignature(BASE_APK) + .run(); + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + + verifyInstalledFiles( + INSTALLED_BASE_APK, + INSTALLED_BASE_APK_FSV_SIG); + verifyInstalledFilesHaveFsverity(); + } + + @Test + public void testInstallBaseWithWrongSignature() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFile(BASE_APK) + .addFile(SPLIT_APK_DM + ".fsv_sig", + BASE_APK + ".fsv_sig") + .runExpectingFailure(); + } + + @Test + public void testInstallBaseWithSplit() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFileAndSignature(BASE_APK) + .addFileAndSignature(SPLIT_APK) + .run(); + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + + verifyInstalledFiles( + INSTALLED_BASE_APK, + INSTALLED_BASE_APK_FSV_SIG, + INSTALLED_SPLIT_APK, + INSTALLED_SPLIT_APK_FSV_SIG); + verifyInstalledFilesHaveFsverity(); + } + + @Test + public void testInstallBaseWithDm() throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFileAndSignature(BASE_APK) + .addFileAndSignature(BASE_APK_DM) + .run(); + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + + verifyInstalledFiles( + INSTALLED_BASE_APK, + INSTALLED_BASE_APK_FSV_SIG, + INSTALLED_BASE_DM, + INSTALLED_BASE_DM_FSV_SIG); + verifyInstalledFilesHaveFsverity(); + } + + @Test + public void testInstallEverything() throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFileAndSignature(BASE_APK) + .addFileAndSignature(BASE_APK_DM) + .addFileAndSignature(SPLIT_APK) + .addFileAndSignature(SPLIT_APK_DM) + .run(); + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + + verifyInstalledFiles( + INSTALLED_BASE_APK, + INSTALLED_BASE_APK_FSV_SIG, + INSTALLED_BASE_DM, + INSTALLED_BASE_DM_FSV_SIG, + INSTALLED_SPLIT_APK, + INSTALLED_SPLIT_APK_FSV_SIG, + INSTALLED_SPLIT_DM, + INSTALLED_SPLIT_DM_FSV_SIG); + verifyInstalledFilesHaveFsverity(); + } + + @Test + public void testInstallSplitOnly() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFileAndSignature(BASE_APK) + .run(); + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + verifyInstalledFiles( + INSTALLED_BASE_APK, + INSTALLED_BASE_APK_FSV_SIG); + + new InstallMultiple() + .inheritFrom(TARGET_PACKAGE) + .addFileAndSignature(SPLIT_APK) + .run(); + + verifyInstalledFiles( + INSTALLED_BASE_APK, + INSTALLED_BASE_APK_FSV_SIG, + INSTALLED_SPLIT_APK, + INSTALLED_SPLIT_APK_FSV_SIG); + verifyInstalledFilesHaveFsverity(); + } + + @Test + public void testInstallSplitOnlyMissingSignature() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFileAndSignature(BASE_APK) + .run(); + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + verifyInstalledFiles( + INSTALLED_BASE_APK, + INSTALLED_BASE_APK_FSV_SIG); + + new InstallMultiple() + .inheritFrom(TARGET_PACKAGE) + .addFile(SPLIT_APK) + .runExpectingFailure(); + } + + @Test + public void testInstallSplitOnlyWithoutBaseSignature() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFile(BASE_APK) + .run(); + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + verifyInstalledFiles(INSTALLED_BASE_APK); + + new InstallMultiple() + .inheritFrom(TARGET_PACKAGE) + .addFileAndSignature(SPLIT_APK) + .run(); + verifyInstalledFiles( + INSTALLED_BASE_APK, + INSTALLED_SPLIT_APK, + INSTALLED_SPLIT_APK_FSV_SIG); + + } + + @Test + public void testInstallOnlyBaseHasFsvSig() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFileAndSignature(BASE_APK) + .addFile(BASE_APK_DM) + .addFile(SPLIT_APK) + .addFile(SPLIT_APK_DM) + .runExpectingFailure(); + } + + @Test + public void testInstallOnlyDmHasFsvSig() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFile(BASE_APK) + .addFileAndSignature(BASE_APK_DM) + .addFile(SPLIT_APK) + .addFile(SPLIT_APK_DM) + .runExpectingFailure(); + } + + @Test + public void testInstallOnlySplitHasFsvSig() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFile(BASE_APK) + .addFile(BASE_APK_DM) + .addFileAndSignature(SPLIT_APK) + .addFile(SPLIT_APK_DM) + .runExpectingFailure(); + } + + @Test + public void testInstallBaseWithFsvSigThenSplitWithout() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFileAndSignature(BASE_APK) + .run(); + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + verifyInstalledFiles( + INSTALLED_BASE_APK, + INSTALLED_BASE_APK_FSV_SIG); + + new InstallMultiple() + .addFile(SPLIT_APK) + .runExpectingFailure(); + } + + @Test + public void testInstallBaseWithoutFsvSigThenSplitWith() + throws DeviceNotAvailableException, FileNotFoundException { + new InstallMultiple() + .addFile(BASE_APK) + .run(); + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + verifyInstalledFiles(INSTALLED_BASE_APK); + + new InstallMultiple() + .addFileAndSignature(SPLIT_APK) + .runExpectingFailure(); + } + + @Test + public void testFsverityFileIsImmutableAndReadable() throws DeviceNotAvailableException { + new InstallMultiple().addFileAndSignature(BASE_APK).run(); + String apkPath = getApkPath(TARGET_PACKAGE); + + assertNotNull(getDevice().getAppPackageInfo(TARGET_PACKAGE)); + expectRemoteCommandToFail("echo -n '' >> " + apkPath); + expectRemoteCommandToSucceed("cat " + apkPath + " > /dev/null"); + } + + @Test + public void testFsverityFailToReadModifiedBlockAtFront() throws DeviceNotAvailableException { + new InstallMultiple().addFileAndSignature(BASE_APK).run(); + String apkPath = getApkPath(TARGET_PACKAGE); + + long apkSize = getFileSizeInBytes(apkPath); + long offsetFirstByte = 0; + + // The first two pages should be both readable at first. + assertTrue(canReadByte(apkPath, offsetFirstByte)); + if (apkSize > offsetFirstByte + FSVERITY_PAGE_SIZE) { + assertTrue(canReadByte(apkPath, offsetFirstByte + FSVERITY_PAGE_SIZE)); + } + + // Damage the file directly against the block device. + damageFileAgainstBlockDevice(apkPath, offsetFirstByte); + + // Expect actual read from disk to fail but only at damaged page. + dropCaches(); + assertFalse(canReadByte(apkPath, offsetFirstByte)); + if (apkSize > offsetFirstByte + FSVERITY_PAGE_SIZE) { + long lastByteOfTheSamePage = + offsetFirstByte % FSVERITY_PAGE_SIZE + FSVERITY_PAGE_SIZE - 1; + assertFalse(canReadByte(apkPath, lastByteOfTheSamePage)); + assertTrue(canReadByte(apkPath, lastByteOfTheSamePage + 1)); + } + } + + @Test + public void testFsverityFailToReadModifiedBlockAtBack() throws DeviceNotAvailableException { + new InstallMultiple().addFileAndSignature(BASE_APK).run(); + String apkPath = getApkPath(TARGET_PACKAGE); + + long apkSize = getFileSizeInBytes(apkPath); + long offsetOfLastByte = apkSize - 1; + + // The first two pages should be both readable at first. + assertTrue(canReadByte(apkPath, offsetOfLastByte)); + if (offsetOfLastByte - FSVERITY_PAGE_SIZE > 0) { + assertTrue(canReadByte(apkPath, offsetOfLastByte - FSVERITY_PAGE_SIZE)); + } + + // Damage the file directly against the block device. + damageFileAgainstBlockDevice(apkPath, offsetOfLastByte); + + // Expect actual read from disk to fail but only at damaged page. + dropCaches(); + assertFalse(canReadByte(apkPath, offsetOfLastByte)); + if (offsetOfLastByte - FSVERITY_PAGE_SIZE > 0) { + long firstByteOfTheSamePage = offsetOfLastByte - offsetOfLastByte % FSVERITY_PAGE_SIZE; + assertFalse(canReadByte(apkPath, firstByteOfTheSamePage)); + assertTrue(canReadByte(apkPath, firstByteOfTheSamePage - 1)); + } + } + + private void verifyInstalledFilesHaveFsverity() throws DeviceNotAvailableException { + // Verify that all files are protected by fs-verity + String apkPath = getApkPath(TARGET_PACKAGE); + String appDir = apkPath.substring(0, apkPath.lastIndexOf("/")); + long kTargetOffset = 0; + for (String basename : expectRemoteCommandToSucceed("ls " + appDir).split("\n")) { + if (basename.endsWith(".apk") || basename.endsWith(".dm")) { + String path = appDir + "/" + basename; + damageFileAgainstBlockDevice(path, kTargetOffset); + + // Retry is sometimes needed to pass the test. Package manager may have FD leaks + // (see b/122744005 as example) that prevents the file in question to be evicted + // from filesystem cache. Forcing GC workarounds the problem. + int retry = 5; + for (; retry > 0; retry--) { + dropCaches(); + if (!canReadByte(path, kTargetOffset)) { + break; + } + try { + Thread.sleep(1000); + String pid = expectRemoteCommandToSucceed("pidof system_server"); + mDevice.executeShellV2Command("kill -10 " + pid); // force GC + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + } + assertTrue("Read from " + path + " should fail", retry > 0); + } + } + } + + private void verifyInstalledFiles(String... filenames) throws DeviceNotAvailableException { + String apkPath = getApkPath(TARGET_PACKAGE); + String appDir = apkPath.substring(0, apkPath.lastIndexOf("/")); + HashSet<String> actualFiles = new HashSet<>(Arrays.asList( + expectRemoteCommandToSucceed("ls " + appDir).split("\n"))); + assertTrue(actualFiles.remove("lib")); + assertTrue(actualFiles.remove("oat")); + + HashSet<String> expectedFiles = new HashSet<>(Arrays.asList(filenames)); + assertEquals(expectedFiles, actualFiles); + } + + private void damageFileAgainstBlockDevice(String path, long offsetOfTargetingByte) + throws DeviceNotAvailableException { + assertTrue(path.startsWith("/data/")); + ITestDevice.MountPointInfo mountPoint = mDevice.getMountPointInfo("/data"); + expectRemoteCommandToSucceed(String.join(" ", DAMAGING_EXECUTABLE, + mountPoint.filesystem, path, Long.toString(offsetOfTargetingByte))); + } + + private String getApkPath(String packageName) throws DeviceNotAvailableException { + String line = expectRemoteCommandToSucceed("pm path " + packageName + " | grep base.apk"); + int index = line.trim().indexOf(":"); + assertTrue(index >= 0); + return line.substring(index + 1); + } + + private long getFileSizeInBytes(String packageName) throws DeviceNotAvailableException { + return Long.parseLong(expectRemoteCommandToSucceed("stat -c '%s' " + packageName).trim()); + } + + private void dropCaches() throws DeviceNotAvailableException { + expectRemoteCommandToSucceed("sync && echo 1 > /proc/sys/vm/drop_caches"); + } + + private boolean canReadByte(String filePath, long offset) throws DeviceNotAvailableException { + CommandResult result = mDevice.executeShellV2Command( + "dd if=" + filePath + " bs=1 count=1 skip=" + Long.toString(offset)); + return result.getStatus() == CommandStatus.SUCCESS; + } + + private String expectRemoteCommandToSucceed(String cmd) throws DeviceNotAvailableException { + CommandResult result = mDevice.executeShellV2Command(cmd); + assertEquals("`" + cmd + "` failed: " + result.getStderr(), CommandStatus.SUCCESS, + result.getStatus()); + return result.getStdout(); + } + + private void expectRemoteCommandToFail(String cmd) throws DeviceNotAvailableException { + CommandResult result = mDevice.executeShellV2Command(cmd); + assertTrue("Unexpected success from `" + cmd + "`: " + result.getStderr(), + result.getStatus() != CommandStatus.SUCCESS); + } + + private class InstallMultiple extends BaseInstallMultiple<InstallMultiple> { + InstallMultiple() { + super(getDevice(), getBuild()); + } + + InstallMultiple addFileAndSignature(String filename) { + try { + addFile(filename); + addFile(filename + ".fsv_sig"); + } catch (FileNotFoundException e) { + fail("Missing test file: " + e); + } + return this; + } + } +} diff --git a/tests/ApkVerityTest/src/com/android/apkverity/BaseInstallMultiple.java b/tests/ApkVerityTest/src/com/android/apkverity/BaseInstallMultiple.java new file mode 100644 index 000000000000..02e73d157dde --- /dev/null +++ b/tests/ApkVerityTest/src/com/android/apkverity/BaseInstallMultiple.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2019 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.apkverity; + +import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; +import com.android.tradefed.build.IBuildInfo; +import com.android.tradefed.device.DeviceNotAvailableException; +import com.android.tradefed.device.ITestDevice; + +import junit.framework.TestCase; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Base class for invoking the install-multiple command via ADB. Subclass this for less typing: + * + * <code> private class InstallMultiple extends BaseInstallMultiple<InstallMultiple> { public + * InstallMultiple() { super(getDevice(), null); } } </code> + */ +/*package*/ class BaseInstallMultiple<T extends BaseInstallMultiple<?>> { + + private final ITestDevice mDevice; + private final IBuildInfo mBuild; + + private final List<String> mArgs = new ArrayList<>(); + private final Map<File, String> mFileToRemoteMap = new HashMap<>(); + + /*package*/ BaseInstallMultiple(ITestDevice device, IBuildInfo buildInfo) { + mDevice = device; + mBuild = buildInfo; + addArg("-g"); + } + + T addArg(String arg) { + mArgs.add(arg); + return (T) this; + } + + T addFile(String filename) throws FileNotFoundException { + return addFile(filename, filename); + } + + T addFile(String filename, String remoteName) throws FileNotFoundException { + CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuild); + mFileToRemoteMap.put(buildHelper.getTestFile(filename), remoteName); + return (T) this; + } + + T inheritFrom(String packageName) { + addArg("-r"); + addArg("-p " + packageName); + return (T) this; + } + + void run() throws DeviceNotAvailableException { + run(true); + } + + void runExpectingFailure() throws DeviceNotAvailableException { + run(false); + } + + private void run(boolean expectingSuccess) throws DeviceNotAvailableException { + final ITestDevice device = mDevice; + + // Create an install session + final StringBuilder cmd = new StringBuilder(); + cmd.append("pm install-create"); + for (String arg : mArgs) { + cmd.append(' ').append(arg); + } + + String result = device.executeShellCommand(cmd.toString()); + TestCase.assertTrue(result, result.startsWith("Success")); + + final int start = result.lastIndexOf("["); + final int end = result.lastIndexOf("]"); + int sessionId = -1; + try { + if (start != -1 && end != -1 && start < end) { + sessionId = Integer.parseInt(result.substring(start + 1, end)); + } + } catch (NumberFormatException e) { + throw new IllegalStateException("Failed to parse install session: " + result); + } + if (sessionId == -1) { + throw new IllegalStateException("Failed to create install session: " + result); + } + + // Push our files into session. Ideally we'd use stdin streaming, + // but ddmlib doesn't support it yet. + for (final Map.Entry<File, String> entry : mFileToRemoteMap.entrySet()) { + final File file = entry.getKey(); + final String remoteName = entry.getValue(); + final String remotePath = "/data/local/tmp/" + file.getName(); + if (!device.pushFile(file, remotePath)) { + throw new IllegalStateException("Failed to push " + file); + } + + cmd.setLength(0); + cmd.append("pm install-write"); + cmd.append(' ').append(sessionId); + cmd.append(' ').append(remoteName); + cmd.append(' ').append(remotePath); + + result = device.executeShellCommand(cmd.toString()); + TestCase.assertTrue(result, result.startsWith("Success")); + } + + // Everything staged; let's pull trigger + cmd.setLength(0); + cmd.append("pm install-commit"); + cmd.append(' ').append(sessionId); + + result = device.executeShellCommand(cmd.toString()); + if (expectingSuccess) { + TestCase.assertTrue(result, result.contains("Success")); + } else { + TestCase.assertFalse(result, result.contains("Success")); + } + } +} diff --git a/tests/ApkVerityTest/testdata/Android.bp b/tests/ApkVerityTest/testdata/Android.bp new file mode 100644 index 000000000000..c10b0cef21d7 --- /dev/null +++ b/tests/ApkVerityTest/testdata/Android.bp @@ -0,0 +1,77 @@ +// Copyright (C) 2019 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. + +filegroup { + name: "ApkVerityTestKeyPem", + srcs: ["ApkVerityTestKey.pem"], +} + +filegroup { + name: "ApkVerityTestCertPem", + srcs: ["ApkVerityTestCert.pem"], +} + +filegroup { + name: "ApkVerityTestCertDer", + srcs: ["ApkVerityTestCert.der"], +} + +filegroup { + name: "ApkVerityTestAppDm", + srcs: ["ApkVerityTestApp.dm"], +} + +filegroup { + name: "ApkVerityTestAppSplitDm", + srcs: ["ApkVerityTestAppSplit.dm"], +} + +genrule_defaults { + name: "apk_verity_sig_gen_default", + tools: ["fsverity"], + tool_files: [":ApkVerityTestKeyPem", ":ApkVerityTestCertPem"], + cmd: "$(location fsverity) sign $(in) $(out) " + + "--key=$(location :ApkVerityTestKeyPem) " + + "--cert=$(location :ApkVerityTestCertPem) " + + "> /dev/null", +} + +genrule { + name: "ApkVerityTestAppFsvSig", + defaults: ["apk_verity_sig_gen_default"], + srcs: [":ApkVerityTestApp"], + out: ["ApkVerityTestApp.apk.fsv_sig"], +} + +genrule { + name: "ApkVerityTestAppDmFsvSig", + defaults: ["apk_verity_sig_gen_default"], + srcs: [":ApkVerityTestAppDm"], + out: ["ApkVerityTestApp.dm.fsv_sig"], +} + +genrule { + name: "ApkVerityTestAppSplitFsvSig", + defaults: ["apk_verity_sig_gen_default"], + srcs: [":ApkVerityTestAppSplit"], + out: ["ApkVerityTestAppSplit.apk.fsv_sig"], +} + +genrule { + name: "ApkVerityTestAppSplitDmFsvSig", + defaults: ["apk_verity_sig_gen_default"], + srcs: [":ApkVerityTestAppSplitDm"], + out: ["ApkVerityTestAppSplit.dm.fsv_sig"], +} + diff --git a/tests/ApkVerityTest/testdata/ApkVerityTestApp.dm b/tests/ApkVerityTest/testdata/ApkVerityTestApp.dm Binary files differnew file mode 100644 index 000000000000..e53a86131366 --- /dev/null +++ b/tests/ApkVerityTest/testdata/ApkVerityTestApp.dm diff --git a/tests/ApkVerityTest/testdata/ApkVerityTestAppSplit.dm b/tests/ApkVerityTest/testdata/ApkVerityTestAppSplit.dm Binary files differnew file mode 100644 index 000000000000..75396f1ba730 --- /dev/null +++ b/tests/ApkVerityTest/testdata/ApkVerityTestAppSplit.dm diff --git a/tests/ApkVerityTest/testdata/ApkVerityTestCert.der b/tests/ApkVerityTest/testdata/ApkVerityTestCert.der Binary files differnew file mode 100644 index 000000000000..fe9029b53aa1 --- /dev/null +++ b/tests/ApkVerityTest/testdata/ApkVerityTestCert.der diff --git a/tests/ApkVerityTest/testdata/ApkVerityTestCert.pem b/tests/ApkVerityTest/testdata/ApkVerityTestCert.pem new file mode 100644 index 000000000000..6c0b7b1f635a --- /dev/null +++ b/tests/ApkVerityTest/testdata/ApkVerityTestCert.pem @@ -0,0 +1,30 @@ +-----BEGIN CERTIFICATE----- +MIIFLjCCAxagAwIBAgIJAKZbtMlZZwtdMA0GCSqGSIb3DQEBCwUAMCwxCzAJBgNV +BAYTAlVTMQswCQYDVQQIDAJDQTEQMA4GA1UECgwHQW5kcm9pZDAeFw0xODEyMTky +MTA5MzVaFw0xOTAxMTgyMTA5MzVaMCwxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJD +QTEQMA4GA1UECgwHQW5kcm9pZDCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC +ggIBAKnrw4WiFgFBq6vXqcLc97iwvcYPZmeIjQqYRF+CHwXBXx8IyDlMfPrgyIYo +ZLkosnUK/Exuypdu6UEtdqtYPknC6w9z4YkxqsKtyxyB1b13ptcTHh3bf2N8bqGr +8gWWLxj0QjumCtFi7Z/TCwB5t3b3gtC+0jVfABSWrm5PNkgk7jIP+4KeYLDCDfiJ +XH3uHu6OASiSHTOnrmLWSaSw0y6G4OFthHqQnMywasly0r6m+Mif+K0ZUV7hBRi/ +SfqcJ1HTCXTJMskEyV6Qx2sHF/VbK2gdUv56z6OVRNSs/FxPBiWVMuZZKh1FpBVI +gbGxusf2Awwtc+Soxr4/P1YFcrwfA/ff9FK3Yg/Cd3ZMGbzUkbEMEkE5BW7Gbjmx +wz3mYTiRfa2L/Bl4MiMqNi0tfORLkmg+V/EItzfhZ/HsXMOCBsnuj4KnFslmbamz +t9opypj2JLGk+lXhZ5gFNFw8tYH1AnG1AIXe5u+6Fq2nQ1y/ncGUTR5Sw4de/Gee +C0UgR+KiFEdKupMKbXgSKl+0QPz/i2eSpcDOKMwZ4WiNrkbccbCyr38so+j5DfWF +IeZA9a/IlysA6G8yU2TfXBc65VCIEQRJOQdUOZFDO8OSoqGP+fbA6edpmovGw+TH +sM/NkmpEXpQm7BVOI4oVjdf4pKPp0zaW2YcaA3xU2w6eF17pAgMBAAGjUzBRMB0G +A1UdDgQWBBRGpHYy7yiLEYalGuF1va6zJKGD/zAfBgNVHSMEGDAWgBRGpHYy7yiL +EYalGuF1va6zJKGD/zAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IC +AQAao6ZBM122F0pYb2QLahIyyGEr3LfSdBGID4068pVik4ncIefFz36Xf9AFxRQd +KHmwRYNPHiLRIEGdtqplC5pZDeHz41txIArNIZKzDWOYtdcFyCz8umuj912BmsoM +YUQhT6F1sX53SWcKxEP/aJ2kltSlPFX99e3Vx9eRkceV1oe2NM6ZG8hnYCfCAMeJ +jRTpbqCGaAsEHFtIx6wt3zEtUXIVg4aYFQs/qjTjeP8ByIj0b4lZrceEoTeRimuj ++4aAI+jBxLkwaN3hseQHzRNpgPehIVV/0RU92yzOD/WN4YwE6rwjKEI1lihHNBDa ++DwGtGbHmIUzjW1qArig+mzUIhfYIJAxrx20ynPz/Q+C7+iXhTDAYQlxTle0pX8m +yM2DUdPo97eLOzQ4JDHxtcN3ntTEJKKvrmzKvWuxy/yoLwS7MtLH6RETTHabH3Qd +CP83X7z8zTyxgPxHdfHo9sgR/4C9RHGJx4OpBTQaiqfjSpDqJSIQdbrHGOQDgYwL +KQyiQuhukmNgRCB6dJoZJ/MyaNuMsXV9QobsDHW1oSuCvPAihVoWHJxt8m4Ma0jJ +EIbEPT2Umw1F/P+CeXnVQwhPvzQKHCa+6cC/YdjTqIKLmQV8X3HUBUIMhP2JGDic +MnUipTm/RwWZVOjCJaFqk5sVq3L0Lyd0XVUWSK1a4IcrsA== +-----END CERTIFICATE----- diff --git a/tests/ApkVerityTest/testdata/ApkVerityTestKey.pem b/tests/ApkVerityTest/testdata/ApkVerityTestKey.pem new file mode 100644 index 000000000000..f0746c162421 --- /dev/null +++ b/tests/ApkVerityTest/testdata/ApkVerityTestKey.pem @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCp68OFohYBQaur +16nC3Pe4sL3GD2ZniI0KmERfgh8FwV8fCMg5THz64MiGKGS5KLJ1CvxMbsqXbulB +LXarWD5JwusPc+GJMarCrcscgdW9d6bXEx4d239jfG6hq/IFli8Y9EI7pgrRYu2f +0wsAebd294LQvtI1XwAUlq5uTzZIJO4yD/uCnmCwwg34iVx97h7ujgEokh0zp65i +1kmksNMuhuDhbYR6kJzMsGrJctK+pvjIn/itGVFe4QUYv0n6nCdR0wl0yTLJBMle +kMdrBxf1WytoHVL+es+jlUTUrPxcTwYllTLmWSodRaQVSIGxsbrH9gMMLXPkqMa+ +Pz9WBXK8HwP33/RSt2IPwnd2TBm81JGxDBJBOQVuxm45scM95mE4kX2ti/wZeDIj +KjYtLXzkS5JoPlfxCLc34Wfx7FzDggbJ7o+CpxbJZm2ps7faKcqY9iSxpPpV4WeY +BTRcPLWB9QJxtQCF3ubvuhatp0Ncv53BlE0eUsOHXvxnngtFIEfiohRHSrqTCm14 +EipftED8/4tnkqXAzijMGeFoja5G3HGwsq9/LKPo+Q31hSHmQPWvyJcrAOhvMlNk +31wXOuVQiBEESTkHVDmRQzvDkqKhj/n2wOnnaZqLxsPkx7DPzZJqRF6UJuwVTiOK +FY3X+KSj6dM2ltmHGgN8VNsOnhde6QIDAQABAoICAGT21tWnisWyXKwd2BwWKgeO +1SRDcEiihZO/CBlr+rzzum55TGdngHedauj0RW0Ttn3/SgysZCp415ZHylRjeZdg +f0VOSLu5TEqi86X7q6IJ35O6I1IAY4AcpqvfvE3/f/qm4FgLADCMRL+LqeTdbdr9 +lLguOj9GNIkHQ5v96zYQ44vRnVNugetlUuHT1KZq/+wlaqDNuRZBU0gdJeL6wnDJ +6gNojKg7F0A0ry8F0B1Cn16uVxebjJMAx4N93hpQALkI2XyQNGHnOzO6eROqQl0i +j/csPW1CUfBUOHLaWpUKy483SOhAINsFz0pqK84G2gIItqTcuRksA/N1J1AYqqQO ++/8IK5Mb9j0RaYYrBG83luGCWYauAsWg2Yol6fUGju8IY/zavOaES42XogY588Ad +JzW+njjxXcnoD/u5keWrGwbPdGfoaLLg4eMlRBT4yNicyT04knXjFG4QTfLY5lF/ +VKdvZk6RMoCLdAtgN6EKHtcwuoYR967otsbavshngZ9HE/ic5/TdNFCBjxs6q9bm +docC4CLHU/feXvOCYSnIfUpDzEPV96Gbk6o0qeYn3RUSGzRpXQHxXXfszEESUWnd +2rtfXxqA7C5n8CshBfKJND7/LKRGpBRaYWJtc4hFmo8prhXfOb40PEZNlx8mcsEz +WYZpmvFQHU8+bZIm0a5RAoIBAQDaCAje9xLKN1CYzygA/U3x2CsGuWWyh9xM1oR5 +5t+nn0EeIMrzGuHrD4hdbZiTiJcO5dpSg/3dssc/QLJEdv+BoMEgSYTf3TX03dIb +kSlj+ONobejO4nVoUP0axTvVe/nuMYvLguTM6OCFvgV752TFxVyVHl6RM+rQYGCl +ajbBCsCRg4QgpZ/RHWf+3KMJunzwWBlsAXcjOudneYqEl713h/q1lc5cONIglQDU +E+bc5q6q++c/H8dYaWq4QE4CQU8wsq77/bZk8z1jheOV0HkwaH5ShtKD7bk/4MA9 +jWQUDW6/LRXkNiPExsAZxnP3mxhtUToWq1nedF6kPmNBko+9AoIBAQDHgvAql6i7 +osTYUcY/GldPmnrvfqbKmD0nI8mnaJfN2vGwjB/ol3lm+pviKIQ4ER80xsdn4xK0 +2cC9OdkY2UX7zilKohxvKVsbVOYpUwfpoRQO1Euddb6iAMqgGDyDzRBDTzTx5pB5 +XL9B/XuJVCMkjsNdD9iEbjdY1Epv7kYf53zfvrXdqv24uSNAszPFBLLPHSC9yONE +a/t3mHGZ2cjr52leGNGY7ib6GNGBUeA34SM9g97tU9pAgy712RfZhH6fA93CLk6T +DKoch56YId71vZt2J0Lrk4TWnnpidSoRmzKfVIJwjCmgYbI+2eDp7h0Z0DnDbji6 +9BPt3RWsoZidAoIBAA2A7+O3U7+Ye3JraiPdjGVNKSUKeIT9KyTLKHtQVEvSbjsK +dudlo9ZmKOD4d7mzfP+cNtBjgmanuvVs8V2SLTL/HNb+Fq+yyLO4xVmVvQWHFbaT +EBc4KWNjmLl+u7z2J72b7feVzMvwJG/EHBzXcQNavOgzcFH38DQls/aqxGdiXhjl +F1raRzKxao57ZdGlbjWIj1KEKLfS3yAmg/DAYSi1EE8MzzIhBsqjz+BStzq5Qtou +Ld1X/4W3SbfNq8cx+lCe0H2k8hYAhq3STg0qU0cvQZuk5Abtw0p0hhOJ3UfsqQ5I +IZH31HFMiftOskIEphenLzzWMgO4G2B6yLT3+dUCggEAOLF1i7Ti5sbfBtVd70qN +6vnr2yhzPvi5z+h0ghTPpliD+3YmDxMUFXY7W63FvKTo6DdgLJ4zD58dDOhmT5BW +ObKguyuLxu7Ki965NJ76jaIPMBOVlR4DWMe+zHV2pMFd0LKuSdsJzOLVGmxscV6u +SdIjo8s/7InhQmW47UuZM7G1I2NvDJltVdOON/F0UZT/NqmBR0zRf/zrTVXNWjmv +xZFRuMJ2tO1fuAvbZNMeUuKv/+f8LhZ424IrkwLoqw/iZ09S8b306AZeRJMpNvPR +BqWlipKnioe15MLN5jKDDNO8M9hw5Ih/v6pjW0bQicj3DgHEmEs25bE8BIihgxe8 +ZQKCAQEAsWKsUv13OEbYYAoJgbzDesWF9NzamFB0NLyno9SChvFPH/d8RmAuti7Y +BQUoBswLK24DF/TKf1YocsZq8tu+pnv0Nx1wtK4K+J3A1BYDm7ElpO3Km+HPUBtf +C9KGT5hotlMQVTpYSDG/QeWbfl4UnNZcbg8pmv38NwV1eDoVDfaVrRYJzQn75+Tf +s/WUq1x5PElR/4pNIU2i6pJGd6FimhRweJu/INR36spWmbMRNX8fyXx+9EBqMbVp +vS2xGgxxQT6bAvBfRlpgi87T9v5Gqoy6/jM/wX9smH9PfUV1vK32n3Zrbd46gwZW +p2aUlQOLXU9SjQTirZbdCZP0XHtFsg== +-----END PRIVATE KEY----- diff --git a/tests/ApkVerityTest/testdata/README.md b/tests/ApkVerityTest/testdata/README.md new file mode 100644 index 000000000000..163cb183a5ad --- /dev/null +++ b/tests/ApkVerityTest/testdata/README.md @@ -0,0 +1,13 @@ +This test only runs on rooted / debuggable device. + +The test tries to install subsets of base.{apk,dm}, split.{apk,dm} and their +corresponding .fsv_sig files (generated by build rule). If installed, the +tests also tries to tamper with the file at absolute disk offset to verify +if fs-verity is effective. + +How to generate dex metadata (.dm) +================================== + + adb shell profman --generate-test-profile=/data/local/tmp/primary.prof + adb pull /data/local/tmp/primary.prof + zip foo.dm primary.prof diff --git a/tests/FlickerTests/AndroidManifest.xml b/tests/FlickerTests/AndroidManifest.xml index 9b73abfd6908..91fb7c12b392 100644 --- a/tests/FlickerTests/AndroidManifest.xml +++ b/tests/FlickerTests/AndroidManifest.xml @@ -21,6 +21,8 @@ <!-- Read and write traces from external storage --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> + <!-- Write secure settings --> + <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" /> <!-- Capture screen contents --> <uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" /> <!-- Enable / Disable tracing !--> diff --git a/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java b/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java index 79f5095010e8..06b58fda5b7d 100644 --- a/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java +++ b/tests/PackageWatchdog/src/com/android/server/PackageWatchdogTest.java @@ -626,11 +626,40 @@ public class PackageWatchdogTest { // Verify that health check is failed assertThat(observer.mMitigatedPackages).containsExactly(APP_A); - // Then clear failed packages and start observing a random package so requests are synced - // and PackageWatchdog#onSupportedPackages is called and APP_A has a chance to fail again - // this time due to package expiry. + // Clear failed packages and forward time to expire the observation duration observer.mMitigatedPackages.clear(); - watchdog.startObservingHealth(observer, Arrays.asList(APP_B), LONG_DURATION); + moveTimeForwardAndDispatch(LONG_DURATION); + + // Verify that health check failure is not notified again + assertThat(observer.mMitigatedPackages).isEmpty(); + } + + /** + * Tests failure when health check duration is different from package observation duration + * Failure is also notified only once. + */ + @Test + public void testExplicitHealthCheckFailureAfterExpiry() { + TestController controller = new TestController(); + PackageWatchdog watchdog = createWatchdog(controller, true /* withPackagesReady */); + TestObserver observer = new TestObserver(OBSERVER_NAME_1, + PackageHealthObserverImpact.USER_IMPACT_MEDIUM); + + // Start observing with explicit health checks for APP_A and + // package observation duration == SHORT_DURATION / 2 + // health check duration == SHORT_DURATION (set by default in the TestController) + controller.setSupportedPackages(Arrays.asList(APP_A)); + watchdog.startObservingHealth(observer, Arrays.asList(APP_A), SHORT_DURATION / 2); + + // Forward time to expire the observation duration + moveTimeForwardAndDispatch(SHORT_DURATION / 2); + + // Verify that health check is failed + assertThat(observer.mMitigatedPackages).containsExactly(APP_A); + + // Clear failed packages and forward time to expire the health check duration + observer.mMitigatedPackages.clear(); + moveTimeForwardAndDispatch(SHORT_DURATION); // Verify that health check failure is not notified again assertThat(observer.mMitigatedPackages).isEmpty(); diff --git a/tools/protologtool/src/com/android/protolog/tool/CodeUtils.kt b/tools/protologtool/src/com/android/protolog/tool/CodeUtils.kt index 5c921612df45..cb2950802c5d 100644 --- a/tools/protologtool/src/com/android/protolog/tool/CodeUtils.kt +++ b/tools/protologtool/src/com/android/protolog/tool/CodeUtils.kt @@ -20,6 +20,7 @@ import com.github.javaparser.ast.CompilationUnit import com.github.javaparser.ast.ImportDeclaration import com.github.javaparser.ast.expr.BinaryExpr import com.github.javaparser.ast.expr.Expression +import com.github.javaparser.ast.expr.MethodCallExpr import com.github.javaparser.ast.expr.StringLiteralExpr object CodeUtils { @@ -27,8 +28,9 @@ object CodeUtils { * Returns a stable hash of a string. * We reimplement String::hashCode() for readability reasons. */ - fun hash(str: String, level: LogLevel): Int { - return (level.name + str).map { c -> c.toInt() }.reduce { h, c -> h * 31 + c } + fun hash(position: String, messageString: String, logLevel: LogLevel, logGroup: LogGroup): Int { + return (position + messageString + logLevel.name + logGroup.name) + .map { c -> c.toInt() }.reduce { h, c -> h * 31 + c } } fun isWildcardStaticImported(code: CompilationUnit, className: String): Boolean { @@ -71,4 +73,11 @@ object CodeUtils { "or concatenation of string literals.", expr) } } + + fun getPositionString(call: MethodCallExpr, fileName: String): String { + return when { + call.range.isPresent -> "$fileName:${call.range.get().begin.line}" + else -> fileName + } + } } diff --git a/tools/protologtool/src/com/android/protolog/tool/Constants.kt b/tools/protologtool/src/com/android/protolog/tool/Constants.kt index 83b3c00ebc28..aa3e00f2f4db 100644 --- a/tools/protologtool/src/com/android/protolog/tool/Constants.kt +++ b/tools/protologtool/src/com/android/protolog/tool/Constants.kt @@ -19,6 +19,6 @@ package com.android.protolog.tool object Constants { const val NAME = "protologtool" const val VERSION = "1.0.0" - const val IS_LOG_TO_ANY_METHOD = "isLogToAny" + const val IS_ENABLED_METHOD = "isEnabled" const val ENUM_VALUES_METHOD = "values" } diff --git a/tools/protologtool/src/com/android/protolog/tool/ProtoLogTool.kt b/tools/protologtool/src/com/android/protolog/tool/ProtoLogTool.kt index 9678ec3a02ba..53834a69ef9f 100644 --- a/tools/protologtool/src/com/android/protolog/tool/ProtoLogTool.kt +++ b/tools/protologtool/src/com/android/protolog/tool/ProtoLogTool.kt @@ -49,14 +49,14 @@ object ProtoLogTool { val file = File(path) val text = file.readText() val code = StaticJavaParser.parse(text) + val pack = if (code.packageDeclaration.isPresent) code.packageDeclaration + .get().nameAsString else "" + val newPath = pack.replace('.', '/') + '/' + file.name val outSrc = when { containsProtoLogText(text, command.protoLogClassNameArg) -> - transformer.processClass(text, code) + transformer.processClass(text, newPath, code) else -> text } - val pack = if (code.packageDeclaration.isPresent) code.packageDeclaration - .get().nameAsString else "" - val newPath = pack.replace('.', '/') + '/' + file.name outJar.putNextEntry(ZipEntry(newPath)) outJar.write(outSrc.toByteArray()) outJar.closeEntry() @@ -76,7 +76,11 @@ object ProtoLogTool { val file = File(path) val text = file.readText() if (containsProtoLogText(text, command.protoLogClassNameArg)) { - builder.processClass(StaticJavaParser.parse(text)) + val code = StaticJavaParser.parse(text) + val pack = if (code.packageDeclaration.isPresent) code.packageDeclaration + .get().nameAsString else "" + val newPath = pack.replace('.', '/') + '/' + file.name + builder.processClass(code, newPath) } } val out = FileOutputStream(command.viewerConfigJsonArg) diff --git a/tools/protologtool/src/com/android/protolog/tool/SourceTransformer.kt b/tools/protologtool/src/com/android/protolog/tool/SourceTransformer.kt index c3920780b22a..3f38bc01fc7c 100644 --- a/tools/protologtool/src/com/android/protolog/tool/SourceTransformer.kt +++ b/tools/protologtool/src/com/android/protolog/tool/SourceTransformer.kt @@ -16,7 +16,7 @@ package com.android.protolog.tool -import com.android.protolog.tool.Constants.IS_LOG_TO_ANY_METHOD +import com.android.protolog.tool.Constants.IS_ENABLED_METHOD import com.android.server.protolog.common.LogDataType import com.github.javaparser.StaticJavaParser import com.github.javaparser.ast.CompilationUnit @@ -73,7 +73,8 @@ class SourceTransformer( } val ifStmt: IfStmt if (group.enabled) { - val hash = CodeUtils.hash(messageString, level) + val position = CodeUtils.getPositionString(call, fileName) + val hash = CodeUtils.hash(position, messageString, level, group) val newCall = call.clone() if (!group.textEnabled) { // Remove message string if text logging is not enabled by default. @@ -90,12 +91,12 @@ class SourceTransformer( // Out: ProtoLog.e(GROUP, 1234, 0, null, arg) newCall.arguments.add(2, IntegerLiteralExpr(typeMask)) // Replace call to a stub method with an actual implementation. - // Out: com.android.server.wm.ProtoLogImpl.e(GROUP, 1234, null, arg) + // Out: com.android.server.protolog.ProtoLogImpl.e(GROUP, 1234, null, arg) newCall.setScope(protoLogImplClassNode) - // Create a call to GROUP.isLogAny() - // Out: GROUP.isLogAny() - val isLogAnyExpr = MethodCallExpr(newCall.arguments[0].clone(), - SimpleName(IS_LOG_TO_ANY_METHOD)) + // Create a call to ProtoLogImpl.isEnabled(GROUP) + // Out: com.android.server.protolog.ProtoLogImpl.isEnabled(GROUP) + val isLogEnabled = MethodCallExpr(protoLogImplClassNode, IS_ENABLED_METHOD, + NodeList<Expression>(newCall.arguments[0].clone())) if (argTypes.size != call.arguments.size - 2) { throw InvalidProtoLogCallException( "Number of arguments does not mach format string", call) @@ -120,11 +121,11 @@ class SourceTransformer( } blockStmt.addStatement(ExpressionStmt(newCall)) // Create an IF-statement with the previously created condition. - // Out: if (GROUP.isLogAny()) { + // Out: if (com.android.server.protolog.ProtoLogImpl.isEnabled(GROUP)) { // long protoLogParam0 = arg; - // com.android.server.wm.ProtoLogImpl.e(GROUP, 1234, 0, null, protoLogParam0); + // com.android.server.protolog.ProtoLogImpl.e(GROUP, 1234, 0, null, protoLogParam0); // } - ifStmt = IfStmt(isLogAnyExpr, blockStmt, null) + ifStmt = IfStmt(isLogEnabled, blockStmt, null) } else { // Surround with if (false). val newCall = parentStmt.clone() @@ -212,12 +213,15 @@ class SourceTransformer( StaticJavaParser.parseExpression<FieldAccessExpr>(protoLogImplClassName) private var processedCode: MutableList<String> = mutableListOf() private var offsets: IntArray = IntArray(0) + private var fileName: String = "" fun processClass( code: String, + path: String, compilationUnit: CompilationUnit = StaticJavaParser.parse(code) ): String { + fileName = path processedCode = code.split('\n').toMutableList() offsets = IntArray(processedCode.size) LexicalPreservingPrinter.setup(compilationUnit) diff --git a/tools/protologtool/src/com/android/protolog/tool/ViewerConfigBuilder.kt b/tools/protologtool/src/com/android/protolog/tool/ViewerConfigBuilder.kt index a75b5c9bbe4b..4c4179768b7f 100644 --- a/tools/protologtool/src/com/android/protolog/tool/ViewerConfigBuilder.kt +++ b/tools/protologtool/src/com/android/protolog/tool/ViewerConfigBuilder.kt @@ -32,24 +32,28 @@ class ViewerConfigBuilder( group: LogGroup ) { if (group.enabled) { - val key = CodeUtils.hash(messageString, level) + val position = CodeUtils.getPositionString(call, fileName) + val key = CodeUtils.hash(position, messageString, level, group) if (statements.containsKey(key)) { - if (statements[key] != Triple(messageString, level, group)) { + if (statements[key] != LogCall(messageString, level, group, position)) { throw HashCollisionException( "Please modify the log message \"$messageString\" " + "or \"${statements[key]}\" - their hashes are equal.") } } else { groups.add(group) - statements[key] = Triple(messageString, level, group) + statements[key] = LogCall(messageString, level, group, position) + call.range.isPresent } } } - private val statements: MutableMap<Int, Triple<String, LogLevel, LogGroup>> = mutableMapOf() + private val statements: MutableMap<Int, LogCall> = mutableMapOf() private val groups: MutableSet<LogGroup> = mutableSetOf() + private var fileName: String = "" - fun processClass(unit: CompilationUnit) { + fun processClass(unit: CompilationUnit, fileName: String) { + this.fileName = fileName protoLogCallVisitor.process(unit, this) } @@ -66,11 +70,13 @@ class ViewerConfigBuilder( writer.name(key.toString()) writer.beginObject() writer.name("message") - writer.value(value.first) + writer.value(value.messageString) writer.name("level") - writer.value(value.second.name) + writer.value(value.logLevel.name) writer.name("group") - writer.value(value.third.name) + writer.value(value.logGroup.name) + writer.name("at") + writer.value(value.position) writer.endObject() } writer.endObject() @@ -88,4 +94,11 @@ class ViewerConfigBuilder( stringWriter.buffer.append('\n') return stringWriter.toString() } + + data class LogCall( + val messageString: String, + val logLevel: LogLevel, + val logGroup: LogGroup, + val position: String + ) } diff --git a/tools/protologtool/tests/com/android/protolog/tool/CodeUtilsTest.kt b/tools/protologtool/tests/com/android/protolog/tool/CodeUtilsTest.kt index 337ed995891c..0acbc9074857 100644 --- a/tools/protologtool/tests/com/android/protolog/tool/CodeUtilsTest.kt +++ b/tools/protologtool/tests/com/android/protolog/tool/CodeUtilsTest.kt @@ -27,17 +27,32 @@ import org.junit.Test class CodeUtilsTest { @Test fun hash() { - assertEquals(-1704685243, CodeUtils.hash("test", LogLevel.DEBUG)) + assertEquals(-1259556708, CodeUtils.hash("Test.java:50", "test", + LogLevel.DEBUG, LogGroup("test", true, true, "TAG"))) + } + + @Test + fun hash_changeLocation() { + assertEquals(15793504, CodeUtils.hash("Test.java:10", "test2", + LogLevel.DEBUG, LogGroup("test", true, true, "TAG"))) } @Test fun hash_changeLevel() { - assertEquals(-1176900998, CodeUtils.hash("test", LogLevel.ERROR)) + assertEquals(-731772463, CodeUtils.hash("Test.java:50", "test", + LogLevel.ERROR, LogGroup("test", true, true, "TAG"))) } @Test fun hash_changeMessage() { - assertEquals(-1305634931, CodeUtils.hash("test2", LogLevel.DEBUG)) + assertEquals(-2026343204, CodeUtils.hash("Test.java:50", "test2", + LogLevel.DEBUG, LogGroup("test", true, true, "TAG"))) + } + + @Test + fun hash_changeGroup() { + assertEquals(1607870166, CodeUtils.hash("Test.java:50", "test2", + LogLevel.DEBUG, LogGroup("test2", true, true, "TAG"))) } @Test diff --git a/tools/protologtool/tests/com/android/protolog/tool/SourceTransformerTest.kt b/tools/protologtool/tests/com/android/protolog/tool/SourceTransformerTest.kt index d6e4a36dc3da..f221fbd216b9 100644 --- a/tools/protologtool/tests/com/android/protolog/tool/SourceTransformerTest.kt +++ b/tools/protologtool/tests/com/android/protolog/tool/SourceTransformerTest.kt @@ -78,7 +78,7 @@ class SourceTransformerTest { class Test { void test() { - if (TEST_GROUP.isLogToAny()) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, 835524026, 9, "test %d %f", protoLogParam0, protoLogParam1); } + if (org.example.ProtoLogImpl.isEnabled(TEST_GROUP)) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, 1922613844, 9, "test %d %f", protoLogParam0, protoLogParam1); } } } """.trimIndent() @@ -88,7 +88,7 @@ class SourceTransformerTest { class Test { void test() { - if (TEST_GROUP.isLogToAny()) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; String protoLogParam2 = String.valueOf("test"); org.example.ProtoLogImpl.w(TEST_GROUP, -986393606, 9, "test %d %f " + "abc %s\n test", protoLogParam0, protoLogParam1, protoLogParam2); + if (org.example.ProtoLogImpl.isEnabled(TEST_GROUP)) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; String protoLogParam2 = String.valueOf("test"); org.example.ProtoLogImpl.w(TEST_GROUP, 805272208, 9, "test %d %f " + "abc %s\n test", protoLogParam0, protoLogParam1, protoLogParam2); } } @@ -100,8 +100,8 @@ class SourceTransformerTest { class Test { void test() { - if (TEST_GROUP.isLogToAny()) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, 835524026, 9, "test %d %f", protoLogParam0, protoLogParam1); } /* ProtoLog.w(TEST_GROUP, "test %d %f", 100, 0.1); */ if (TEST_GROUP.isLogToAny()) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, 835524026, 9, "test %d %f", protoLogParam0, protoLogParam1); } - if (TEST_GROUP.isLogToAny()) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, 835524026, 9, "test %d %f", protoLogParam0, protoLogParam1); } + if (org.example.ProtoLogImpl.isEnabled(TEST_GROUP)) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, 1922613844, 9, "test %d %f", protoLogParam0, protoLogParam1); } /* ProtoLog.w(TEST_GROUP, "test %d %f", 100, 0.1); */ if (org.example.ProtoLogImpl.isEnabled(TEST_GROUP)) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, 1922613844, 9, "test %d %f", protoLogParam0, protoLogParam1); } + if (org.example.ProtoLogImpl.isEnabled(TEST_GROUP)) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, -154595499, 9, "test %d %f", protoLogParam0, protoLogParam1); } } } """.trimIndent() @@ -111,7 +111,7 @@ class SourceTransformerTest { class Test { void test() { - if (TEST_GROUP.isLogToAny()) { org.example.ProtoLogImpl.w(TEST_GROUP, 1282022424, 0, "test", (Object[]) null); } + if (org.example.ProtoLogImpl.isEnabled(TEST_GROUP)) { org.example.ProtoLogImpl.w(TEST_GROUP, 1913810354, 0, "test", (Object[]) null); } } } """.trimIndent() @@ -121,7 +121,7 @@ class SourceTransformerTest { class Test { void test() { - if (TEST_GROUP.isLogToAny()) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, 835524026, 9, null, protoLogParam0, protoLogParam1); } + if (org.example.ProtoLogImpl.isEnabled(TEST_GROUP)) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; org.example.ProtoLogImpl.w(TEST_GROUP, 1922613844, 9, null, protoLogParam0, protoLogParam1); } } } """.trimIndent() @@ -131,7 +131,7 @@ class SourceTransformerTest { class Test { void test() { - if (TEST_GROUP.isLogToAny()) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; String protoLogParam2 = String.valueOf("test"); org.example.ProtoLogImpl.w(TEST_GROUP, -986393606, 9, null, protoLogParam0, protoLogParam1, protoLogParam2); + if (org.example.ProtoLogImpl.isEnabled(TEST_GROUP)) { long protoLogParam0 = 100; double protoLogParam1 = 0.1; String protoLogParam2 = String.valueOf("test"); org.example.ProtoLogImpl.w(TEST_GROUP, 805272208, 9, null, protoLogParam0, protoLogParam1, protoLogParam2); } } @@ -160,10 +160,13 @@ class SourceTransformerTest { } """.trimIndent() /* ktlint-enable max-line-length */ + + private const val PATH = "com.example.Test.java" } private val processor: ProtoLogCallProcessor = Mockito.mock(ProtoLogCallProcessor::class.java) - private val sourceJarWriter = SourceTransformer("org.example.ProtoLogImpl", processor) + private val implPath = "org.example.ProtoLogImpl" + private val sourceJarWriter = SourceTransformer(implPath, processor) private fun <T> any(type: Class<T>): T = Mockito.any<T>(type) @@ -181,13 +184,13 @@ class SourceTransformerTest { invocation.arguments[0] as CompilationUnit } - val out = sourceJarWriter.processClass(TEST_CODE, code) + val out = sourceJarWriter.processClass(TEST_CODE, PATH, code) code = StaticJavaParser.parse(out) val ifStmts = code.findAll(IfStmt::class.java) assertEquals(1, ifStmts.size) val ifStmt = ifStmts[0] - assertEquals("TEST_GROUP.${Constants.IS_LOG_TO_ANY_METHOD}()", + assertEquals("$implPath.${Constants.IS_ENABLED_METHOD}(TEST_GROUP)", ifStmt.condition.toString()) assertFalse(ifStmt.elseStmt.isPresent) assertEquals(3, ifStmt.thenStmt.childNodes.size) @@ -196,7 +199,7 @@ class SourceTransformerTest { assertEquals("w", methodCall.name.asString()) assertEquals(6, methodCall.arguments.size) assertEquals("TEST_GROUP", methodCall.arguments[0].toString()) - assertEquals("835524026", methodCall.arguments[1].toString()) + assertEquals("1922613844", methodCall.arguments[1].toString()) assertEquals(0b1001.toString(), methodCall.arguments[2].toString()) assertEquals("\"test %d %f\"", methodCall.arguments[3].toString()) assertEquals("protoLogParam0", methodCall.arguments[4].toString()) @@ -223,13 +226,13 @@ class SourceTransformerTest { invocation.arguments[0] as CompilationUnit } - val out = sourceJarWriter.processClass(TEST_CODE_MULTICALLS, code) + val out = sourceJarWriter.processClass(TEST_CODE_MULTICALLS, PATH, code) code = StaticJavaParser.parse(out) val ifStmts = code.findAll(IfStmt::class.java) assertEquals(3, ifStmts.size) val ifStmt = ifStmts[1] - assertEquals("TEST_GROUP.${Constants.IS_LOG_TO_ANY_METHOD}()", + assertEquals("$implPath.${Constants.IS_ENABLED_METHOD}(TEST_GROUP)", ifStmt.condition.toString()) assertFalse(ifStmt.elseStmt.isPresent) assertEquals(3, ifStmt.thenStmt.childNodes.size) @@ -238,7 +241,7 @@ class SourceTransformerTest { assertEquals("w", methodCall.name.asString()) assertEquals(6, methodCall.arguments.size) assertEquals("TEST_GROUP", methodCall.arguments[0].toString()) - assertEquals("835524026", methodCall.arguments[1].toString()) + assertEquals("1922613844", methodCall.arguments[1].toString()) assertEquals(0b1001.toString(), methodCall.arguments[2].toString()) assertEquals("\"test %d %f\"", methodCall.arguments[3].toString()) assertEquals("protoLogParam0", methodCall.arguments[4].toString()) @@ -261,13 +264,13 @@ class SourceTransformerTest { invocation.arguments[0] as CompilationUnit } - val out = sourceJarWriter.processClass(TEST_CODE_MULTILINE, code) + val out = sourceJarWriter.processClass(TEST_CODE_MULTILINE, PATH, code) code = StaticJavaParser.parse(out) val ifStmts = code.findAll(IfStmt::class.java) assertEquals(1, ifStmts.size) val ifStmt = ifStmts[0] - assertEquals("TEST_GROUP.${Constants.IS_LOG_TO_ANY_METHOD}()", + assertEquals("$implPath.${Constants.IS_ENABLED_METHOD}(TEST_GROUP)", ifStmt.condition.toString()) assertFalse(ifStmt.elseStmt.isPresent) assertEquals(4, ifStmt.thenStmt.childNodes.size) @@ -276,7 +279,7 @@ class SourceTransformerTest { assertEquals("w", methodCall.name.asString()) assertEquals(7, methodCall.arguments.size) assertEquals("TEST_GROUP", methodCall.arguments[0].toString()) - assertEquals("-986393606", methodCall.arguments[1].toString()) + assertEquals("805272208", methodCall.arguments[1].toString()) assertEquals(0b001001.toString(), methodCall.arguments[2].toString()) assertEquals("protoLogParam0", methodCall.arguments[4].toString()) assertEquals("protoLogParam1", methodCall.arguments[5].toString()) @@ -298,13 +301,13 @@ class SourceTransformerTest { invocation.arguments[0] as CompilationUnit } - val out = sourceJarWriter.processClass(TEST_CODE_NO_PARAMS, code) + val out = sourceJarWriter.processClass(TEST_CODE_NO_PARAMS, PATH, code) code = StaticJavaParser.parse(out) val ifStmts = code.findAll(IfStmt::class.java) assertEquals(1, ifStmts.size) val ifStmt = ifStmts[0] - assertEquals("TEST_GROUP.${Constants.IS_LOG_TO_ANY_METHOD}()", + assertEquals("$implPath.${Constants.IS_ENABLED_METHOD}(TEST_GROUP)", ifStmt.condition.toString()) assertFalse(ifStmt.elseStmt.isPresent) assertEquals(1, ifStmt.thenStmt.childNodes.size) @@ -313,7 +316,7 @@ class SourceTransformerTest { assertEquals("w", methodCall.name.asString()) assertEquals(5, methodCall.arguments.size) assertEquals("TEST_GROUP", methodCall.arguments[0].toString()) - assertEquals("1282022424", methodCall.arguments[1].toString()) + assertEquals("1913810354", methodCall.arguments[1].toString()) assertEquals(0.toString(), methodCall.arguments[2].toString()) assertEquals(TRANSFORMED_CODE_NO_PARAMS, out) } @@ -332,13 +335,13 @@ class SourceTransformerTest { invocation.arguments[0] as CompilationUnit } - val out = sourceJarWriter.processClass(TEST_CODE, code) + val out = sourceJarWriter.processClass(TEST_CODE, PATH, code) code = StaticJavaParser.parse(out) val ifStmts = code.findAll(IfStmt::class.java) assertEquals(1, ifStmts.size) val ifStmt = ifStmts[0] - assertEquals("TEST_GROUP.${Constants.IS_LOG_TO_ANY_METHOD}()", + assertEquals("$implPath.${Constants.IS_ENABLED_METHOD}(TEST_GROUP)", ifStmt.condition.toString()) assertFalse(ifStmt.elseStmt.isPresent) assertEquals(3, ifStmt.thenStmt.childNodes.size) @@ -347,7 +350,7 @@ class SourceTransformerTest { assertEquals("w", methodCall.name.asString()) assertEquals(6, methodCall.arguments.size) assertEquals("TEST_GROUP", methodCall.arguments[0].toString()) - assertEquals("835524026", methodCall.arguments[1].toString()) + assertEquals("1922613844", methodCall.arguments[1].toString()) assertEquals(0b1001.toString(), methodCall.arguments[2].toString()) assertEquals("null", methodCall.arguments[3].toString()) assertEquals("protoLogParam0", methodCall.arguments[4].toString()) @@ -370,13 +373,13 @@ class SourceTransformerTest { invocation.arguments[0] as CompilationUnit } - val out = sourceJarWriter.processClass(TEST_CODE_MULTILINE, code) + val out = sourceJarWriter.processClass(TEST_CODE_MULTILINE, PATH, code) code = StaticJavaParser.parse(out) val ifStmts = code.findAll(IfStmt::class.java) assertEquals(1, ifStmts.size) val ifStmt = ifStmts[0] - assertEquals("TEST_GROUP.${Constants.IS_LOG_TO_ANY_METHOD}()", + assertEquals("$implPath.${Constants.IS_ENABLED_METHOD}(TEST_GROUP)", ifStmt.condition.toString()) assertFalse(ifStmt.elseStmt.isPresent) assertEquals(4, ifStmt.thenStmt.childNodes.size) @@ -385,7 +388,7 @@ class SourceTransformerTest { assertEquals("w", methodCall.name.asString()) assertEquals(7, methodCall.arguments.size) assertEquals("TEST_GROUP", methodCall.arguments[0].toString()) - assertEquals("-986393606", methodCall.arguments[1].toString()) + assertEquals("805272208", methodCall.arguments[1].toString()) assertEquals(0b001001.toString(), methodCall.arguments[2].toString()) assertEquals("null", methodCall.arguments[3].toString()) assertEquals("protoLogParam0", methodCall.arguments[4].toString()) @@ -408,7 +411,7 @@ class SourceTransformerTest { invocation.arguments[0] as CompilationUnit } - val out = sourceJarWriter.processClass(TEST_CODE, code) + val out = sourceJarWriter.processClass(TEST_CODE, PATH, code) code = StaticJavaParser.parse(out) val ifStmts = code.findAll(IfStmt::class.java) @@ -433,7 +436,7 @@ class SourceTransformerTest { invocation.arguments[0] as CompilationUnit } - val out = sourceJarWriter.processClass(TEST_CODE_MULTILINE, code) + val out = sourceJarWriter.processClass(TEST_CODE_MULTILINE, PATH, code) code = StaticJavaParser.parse(out) val ifStmts = code.findAll(IfStmt::class.java) diff --git a/tools/protologtool/tests/com/android/protolog/tool/ViewerConfigBuilderTest.kt b/tools/protologtool/tests/com/android/protolog/tool/ViewerConfigBuilderTest.kt index f435d4065256..d3f8c767e8b1 100644 --- a/tools/protologtool/tests/com/android/protolog/tool/ViewerConfigBuilderTest.kt +++ b/tools/protologtool/tests/com/android/protolog/tool/ViewerConfigBuilderTest.kt @@ -31,6 +31,10 @@ class ViewerConfigBuilderTest { private val TEST1 = ViewerConfigParser.ConfigEntry("test1", LogLevel.INFO.name, TAG1) private val TEST2 = ViewerConfigParser.ConfigEntry("test2", LogLevel.DEBUG.name, TAG2) private val TEST3 = ViewerConfigParser.ConfigEntry("test3", LogLevel.ERROR.name, TAG2) + private val GROUP1 = LogGroup("TEST_GROUP", true, true, TAG1) + private val GROUP2 = LogGroup("DEBUG_GROUP", true, true, TAG2) + private val GROUP3 = LogGroup("DEBUG_GROUP", true, true, TAG2) + private const val PATH = "/tmp/test.java" } private val processor: ProtoLogCallProcessor = Mockito.mock(ProtoLogCallProcessor::class.java) @@ -50,25 +54,25 @@ class ViewerConfigBuilderTest { val visitor = invocation.arguments[1] as ProtoLogCallVisitor visitor.processCall(MethodCallExpr(), TEST1.messageString, LogLevel.INFO, - LogGroup("TEST_GROUP", true, true, TAG1)) + GROUP1) visitor.processCall(MethodCallExpr(), TEST2.messageString, LogLevel.DEBUG, - LogGroup("DEBUG_GROUP", true, true, TAG2)) + GROUP2) visitor.processCall(MethodCallExpr(), TEST3.messageString, LogLevel.ERROR, - LogGroup("DEBUG_GROUP", true, true, TAG2)) + GROUP3) invocation.arguments[0] as CompilationUnit } - configBuilder.processClass(dummyCompilationUnit) + configBuilder.processClass(dummyCompilationUnit, PATH) val parsedConfig = parseConfig(configBuilder.build()) assertEquals(3, parsedConfig.size) - assertEquals(TEST1, parsedConfig[CodeUtils.hash(TEST1.messageString, - LogLevel.INFO)]) - assertEquals(TEST2, parsedConfig[CodeUtils.hash(TEST2.messageString, - LogLevel.DEBUG)]) - assertEquals(TEST3, parsedConfig[CodeUtils.hash(TEST3.messageString, - LogLevel.ERROR)]) + assertEquals(TEST1, parsedConfig[CodeUtils.hash(PATH, + TEST1.messageString, LogLevel.INFO, GROUP1)]) + assertEquals(TEST2, parsedConfig[CodeUtils.hash(PATH, TEST2.messageString, + LogLevel.DEBUG, GROUP2)]) + assertEquals(TEST3, parsedConfig[CodeUtils.hash(PATH, TEST3.messageString, + LogLevel.ERROR, GROUP3)]) } @Test @@ -78,20 +82,21 @@ class ViewerConfigBuilderTest { val visitor = invocation.arguments[1] as ProtoLogCallVisitor visitor.processCall(MethodCallExpr(), TEST1.messageString, LogLevel.INFO, - LogGroup("TEST_GROUP", true, true, TAG1)) + GROUP1) visitor.processCall(MethodCallExpr(), TEST1.messageString, LogLevel.INFO, - LogGroup("TEST_GROUP", true, true, TAG1)) + GROUP1) visitor.processCall(MethodCallExpr(), TEST1.messageString, LogLevel.INFO, - LogGroup("TEST_GROUP", true, true, TAG1)) + GROUP1) invocation.arguments[0] as CompilationUnit } - configBuilder.processClass(dummyCompilationUnit) + configBuilder.processClass(dummyCompilationUnit, PATH) val parsedConfig = parseConfig(configBuilder.build()) assertEquals(1, parsedConfig.size) - assertEquals(TEST1, parsedConfig[CodeUtils.hash(TEST1.messageString, LogLevel.INFO)]) + assertEquals(TEST1, parsedConfig[CodeUtils.hash(PATH, TEST1.messageString, + LogLevel.INFO, GROUP1)]) } @Test @@ -101,7 +106,7 @@ class ViewerConfigBuilderTest { val visitor = invocation.arguments[1] as ProtoLogCallVisitor visitor.processCall(MethodCallExpr(), TEST1.messageString, LogLevel.INFO, - LogGroup("TEST_GROUP", true, true, TAG1)) + GROUP1) visitor.processCall(MethodCallExpr(), TEST2.messageString, LogLevel.DEBUG, LogGroup("DEBUG_GROUP", false, true, TAG2)) visitor.processCall(MethodCallExpr(), TEST3.messageString, LogLevel.ERROR, @@ -110,11 +115,13 @@ class ViewerConfigBuilderTest { invocation.arguments[0] as CompilationUnit } - configBuilder.processClass(dummyCompilationUnit) + configBuilder.processClass(dummyCompilationUnit, PATH) val parsedConfig = parseConfig(configBuilder.build()) assertEquals(2, parsedConfig.size) - assertEquals(TEST1, parsedConfig[CodeUtils.hash(TEST1.messageString, LogLevel.INFO)]) - assertEquals(TEST3, parsedConfig[CodeUtils.hash(TEST3.messageString, LogLevel.ERROR)]) + assertEquals(TEST1, parsedConfig[CodeUtils.hash(PATH, TEST1.messageString, + LogLevel.INFO, GROUP1)]) + assertEquals(TEST3, parsedConfig[CodeUtils.hash(PATH, TEST3.messageString, + LogLevel.ERROR, LogGroup("DEBUG_GROUP", true, false, TAG2))]) } } diff --git a/tools/validatekeymaps/Main.cpp b/tools/validatekeymaps/Main.cpp index 56a242f1daaf..5ac9dfd2a557 100644 --- a/tools/validatekeymaps/Main.cpp +++ b/tools/validatekeymaps/Main.cpp @@ -137,7 +137,6 @@ static bool validateFile(const char* filename) { } } - log("No errors.\n\n"); return true; } diff --git a/wifi/java/android/net/wifi/WifiScanner.java b/wifi/java/android/net/wifi/WifiScanner.java index 075531ce158e..68948cbbe7a9 100644 --- a/wifi/java/android/net/wifi/WifiScanner.java +++ b/wifi/java/android/net/wifi/WifiScanner.java @@ -17,6 +17,7 @@ package android.net.wifi; import android.Manifest; +import android.annotation.NonNull; import android.annotation.RequiresPermission; import android.annotation.SuppressLint; import android.annotation.SystemApi; @@ -58,13 +59,23 @@ public class WifiScanner { /** 5 GHz band excluding DFS channels */ public static final int WIFI_BAND_5_GHZ = 2; /* 5 GHz band without DFS channels */ /** DFS channels from 5 GHz band only */ - public static final int WIFI_BAND_5_GHZ_DFS_ONLY = 4; /* 5 GHz band with DFS channels */ + public static final int WIFI_BAND_5_GHZ_DFS_ONLY = 4; /* 5 GHz band DFS channels */ + /** + * 2.4Ghz band + DFS channels from 5 GHz band only + * @hide + */ + public static final int WIFI_BAND_24_GHZ_WITH_5GHZ_DFS = 5; /** 5 GHz band including DFS channels */ public static final int WIFI_BAND_5_GHZ_WITH_DFS = 6; /* 5 GHz band with DFS channels */ /** Both 2.4 GHz band and 5 GHz band; no DFS channels */ public static final int WIFI_BAND_BOTH = 3; /* both bands without DFS channels */ /** Both 2.4 GHz band and 5 GHz band; with DFS channels */ public static final int WIFI_BAND_BOTH_WITH_DFS = 7; /* both bands with DFS channels */ + /** + * Max band value + * @hide + */ + public static final int WIFI_BAND_MAX = 8; /** Minimum supported scanning period */ public static final int MIN_SCAN_PERIOD_MS = 1000; /* minimum supported period */ @@ -375,19 +386,27 @@ public class WifiScanner { */ private int mBandScanned; /** all scan results discovered in this scan, sorted by timestamp in ascending order */ - private ScanResult mResults[]; + private final List<ScanResult> mResults; - ScanData() {} + ScanData() { + mResults = new ArrayList<>(); + } public ScanData(int id, int flags, ScanResult[] results) { mId = id; mFlags = flags; - mResults = results; + mResults = new ArrayList<>(Arrays.asList(results)); } /** {@hide} */ public ScanData(int id, int flags, int bucketsScanned, int bandScanned, ScanResult[] results) { + this(id, flags, bucketsScanned, bandScanned, new ArrayList<>(Arrays.asList(results))); + } + + /** {@hide} */ + public ScanData(int id, int flags, int bucketsScanned, int bandScanned, + List<ScanResult> results) { mId = id; mFlags = flags; mBucketsScanned = bucketsScanned; @@ -400,11 +419,9 @@ public class WifiScanner { mFlags = s.mFlags; mBucketsScanned = s.mBucketsScanned; mBandScanned = s.mBandScanned; - mResults = new ScanResult[s.mResults.length]; - for (int i = 0; i < s.mResults.length; i++) { - ScanResult result = s.mResults[i]; - ScanResult newResult = new ScanResult(result); - mResults[i] = newResult; + mResults = new ArrayList<>(); + for (ScanResult scanResult : s.mResults) { + mResults.add(new ScanResult(scanResult)); } } @@ -427,7 +444,14 @@ public class WifiScanner { } public ScanResult[] getResults() { - return mResults; + return mResults.toArray(new ScanResult[0]); + } + + /** {@hide} */ + public void addResults(@NonNull ScanResult[] newResults) { + for (ScanResult result : newResults) { + mResults.add(new ScanResult(result)); + } } /** Implement the Parcelable interface {@hide} */ @@ -437,19 +461,11 @@ public class WifiScanner { /** Implement the Parcelable interface {@hide} */ public void writeToParcel(Parcel dest, int flags) { - if (mResults != null) { - dest.writeInt(mId); - dest.writeInt(mFlags); - dest.writeInt(mBucketsScanned); - dest.writeInt(mBandScanned); - dest.writeInt(mResults.length); - for (int i = 0; i < mResults.length; i++) { - ScanResult result = mResults[i]; - result.writeToParcel(dest, flags); - } - } else { - dest.writeInt(0); - } + dest.writeInt(mId); + dest.writeInt(mFlags); + dest.writeInt(mBucketsScanned); + dest.writeInt(mBandScanned); + dest.writeParcelableList(mResults, 0); } /** Implement the Parcelable interface {@hide} */ @@ -460,11 +476,8 @@ public class WifiScanner { int flags = in.readInt(); int bucketsScanned = in.readInt(); int bandScanned = in.readInt(); - int n = in.readInt(); - ScanResult results[] = new ScanResult[n]; - for (int i = 0; i < n; i++) { - results[i] = ScanResult.CREATOR.createFromParcel(in); - } + List<ScanResult> results = new ArrayList<>(); + in.readParcelableList(results, ScanResult.class.getClassLoader()); return new ScanData(id, flags, bucketsScanned, bandScanned, results); } diff --git a/wifi/tests/src/android/net/wifi/WifiScannerTest.java b/wifi/tests/src/android/net/wifi/WifiScannerTest.java index dd05b47fbd4f..ea136d62b202 100644 --- a/wifi/tests/src/android/net/wifi/WifiScannerTest.java +++ b/wifi/tests/src/android/net/wifi/WifiScannerTest.java @@ -22,7 +22,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; @@ -445,4 +444,37 @@ public class WifiScannerTest { assertEquals(WifiScanner.CMD_STOP_PNO_SCAN, message.what); } + + @Test + public void testScanDataAddResults() throws Exception { + ScanResult scanResult1 = new ScanResult(); + scanResult1.SSID = TEST_SSID_1; + ScanData scanData = new ScanData(0, 0, new ScanResult[]{scanResult1}); + + ScanResult scanResult2 = new ScanResult(); + scanResult2.SSID = TEST_SSID_2; + scanData.addResults(new ScanResult[]{scanResult2}); + + ScanResult[] consolidatedScanResults = scanData.getResults(); + assertEquals(2, consolidatedScanResults.length); + assertEquals(TEST_SSID_1, consolidatedScanResults[0].SSID); + assertEquals(TEST_SSID_2, consolidatedScanResults[1].SSID); + } + + @Test + public void testScanDataParcel() throws Exception { + ScanResult scanResult1 = new ScanResult(); + scanResult1.SSID = TEST_SSID_1; + ScanData scanData = new ScanData(5, 4, new ScanResult[]{scanResult1}); + + Parcel parcel = Parcel.obtain(); + scanData.writeToParcel(parcel, 0); + parcel.setDataPosition(0); // Rewind data position back to the beginning for read. + ScanData readScanData = ScanData.CREATOR.createFromParcel(parcel); + + assertEquals(scanData.getId(), readScanData.getId()); + assertEquals(scanData.getFlags(), readScanData.getFlags()); + assertEquals(scanData.getResults().length, readScanData.getResults().length); + assertEquals(scanData.getResults()[0].SSID, readScanData.getResults()[0].SSID); + } } |