Merge "Add preference controller to launch app-time-spent UI." into pi-dev

This commit is contained in:
TreeHugger Robot
2018-03-24 05:14:32 +00:00
committed by Android (Google) Code Review
5 changed files with 198 additions and 18 deletions

View File

@@ -25,13 +25,13 @@
android:key="header_view"
android:layout="@layout/settings_entity_header"
android:selectable="false"
android:order="-10000"/>
android:order="-10000" />
<com.android.settings.applications.LayoutPreference
android:key="instant_app_buttons"
android:layout="@layout/instant_app_buttons"
android:selectable="false"
android:order="-9999"/>
android:order="-9999" />
<com.android.settings.widget.ActionButtonPreference
android:key="action_buttons"
@@ -39,7 +39,7 @@
<Preference
android:key="notification_settings"
android:title="@string/notifications_label"/>
android:title="@string/notifications_label" />
<com.android.settings.widget.FixedLineSummaryPreference
android:key="permission_settings"
@@ -50,7 +50,7 @@
<Preference
android:key="storage_settings"
android:title="@string/storage_settings"
android:summary="@string/summary_placeholder"/>
android:summary="@string/summary_placeholder" />
<com.android.settings.applications.AppDomainsPreference
android:key="instant_app_launch_supported_domain_urls"
@@ -60,24 +60,29 @@
<Preference
android:key="data_settings"
android:title="@string/data_usage_summary_title"
android:summary="@string/summary_placeholder"/>
android:summary="@string/summary_placeholder" />
<Preference
android:key="time_spent_in_app"
android:title="@string/time_spent_in_app_pref_title"
app:controller="com.android.settings.applications.appinfo.TimeSpentInAppPreferenceController" />
<Preference
android:key="battery"
android:title="@string/power_usage_summary_title"
android:summary="@string/summary_placeholder"/>
android:summary="@string/summary_placeholder" />
<Preference
android:key="preferred_settings"
android:title="@string/launch_by_default"
android:summary="@string/summary_placeholder"
android:selectable="true"/>
android:selectable="true" />
<Preference
android:key="memory"
android:title="@string/memory_settings_title"
android:summary="@string/summary_placeholder"
android:enabled="false"/>
android:enabled="false" />
<!-- Default apps shortcuts -->
<Preference
@@ -146,6 +151,6 @@
<Preference
android:key="app_version"
android:selectable="false"
android:order="9999"/>
android:order="9999" />
</PreferenceScreen>

View File

@@ -45,20 +45,16 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.DeviceAdminAdd;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.applications.manageapplications.ManageApplications;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.EntityHeaderController;
import com.android.settings.widget.PreferenceCategoryController;
import com.android.settings.wrapper.DevicePolicyManagerWrapper;
import com.android.settingslib.RestrictedLockUtils;
@@ -157,7 +153,12 @@ public class AppInfoDashboardFragment extends DashboardFragment
== PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
}
/** Called when the activity is first created. */
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(TimeSpentInAppPreferenceController.class).setPackageName(getPackageName());
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
@@ -191,9 +192,10 @@ public class AppInfoDashboardFragment extends DashboardFragment
@Override
public void onResume() {
super.onResume();
mAppsControlDisallowedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(getActivity(),
final Activity activity = getActivity();
mAppsControlDisallowedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(activity,
UserManager.DISALLOW_APPS_CONTROL, mUserId);
mAppsControlDisallowedBySystem = RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
mAppsControlDisallowedBySystem = RestrictedLockUtils.hasBaseUserRestriction(activity,
UserManager.DISALLOW_APPS_CONTROL, mUserId);
if (!refreshUi()) {
@@ -300,7 +302,7 @@ public class AppInfoDashboardFragment extends DashboardFragment
*
* @return true if packageInfo is available.
*/
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
@VisibleForTesting
boolean ensurePackageInfoAvailable(Activity activity) {
if (mPackageInfo == null) {
mFinishing = true;

View File

@@ -0,0 +1,75 @@
/*
* 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.settings.applications.appinfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import com.android.settings.core.BasePreferenceController;
import java.util.List;
public class TimeSpentInAppPreferenceController extends BasePreferenceController {
@VisibleForTesting
static final Intent SEE_TIME_IN_APP_TEMPLATE =
new Intent("com.android.settings.action.TIME_SPENT_IN_APP");
private final PackageManager mPackageManager;
private Intent mIntent;
private String mPackageName;
public TimeSpentInAppPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mPackageManager = context.getPackageManager();
}
public void setPackageName(String packageName) {
mPackageName = packageName;
mIntent = new Intent(SEE_TIME_IN_APP_TEMPLATE)
.putExtra(Intent.EXTRA_PACKAGE_NAME, mPackageName);
}
@Override
public int getAvailabilityStatus() {
if (TextUtils.isEmpty(mPackageName)) {
return DISABLED_UNSUPPORTED;
}
final List<ResolveInfo> resolved = mPackageManager.queryIntentActivities(mIntent,
0 /* flags */);
if (resolved == null || resolved.isEmpty()) {
return DISABLED_UNSUPPORTED;
}
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final Preference pref = screen.findPreference(getPreferenceKey());
if (pref != null) {
pref.setIntent(mIntent);
}
}
}

View File

@@ -56,7 +56,7 @@ public class PreferenceControllerListHelper {
preferenceMetadata = PreferenceXmlParserUtils.extractMetadata(context, xmlResId,
MetadataFlag.FLAG_NEED_KEY | MetadataFlag.FLAG_NEED_PREF_CONTROLLER);
} catch (IOException | XmlPullParserException e) {
Log.e(TAG, "Failed to parse preference xml for getting controllers");
Log.e(TAG, "Failed to parse preference xml for getting controllers", e);
return controllers;
}

View File

@@ -0,0 +1,98 @@
/*
* 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.settings.applications.appinfo;
import static android.content.Intent.EXTRA_PACKAGE_NAME;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPackageManager;
@RunWith(SettingsRobolectricTestRunner.class)
public class TimeSpentInAppPreferenceControllerTest {
private static final String TEST_KEY = "test_tey";
private static final Intent TEST_INTENT = new Intent(
TimeSpentInAppPreferenceController.SEE_TIME_IN_APP_TEMPLATE)
.putExtra(EXTRA_PACKAGE_NAME, "com.android.settings");
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private ShadowPackageManager mPackageManager;
private TimeSpentInAppPreferenceController mController;
private Preference mPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPackageManager = Shadows.shadowOf(mContext.getPackageManager());
mController = new TimeSpentInAppPreferenceController(mContext, TEST_KEY);
mPreference = new Preference(mContext);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
}
@Test
public void noPackageName_shouldBeDisabled() {
mController.setPackageName(null);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
}
@Test
public void noIntentHandler_shouldBeDisabled() {
mController.setPackageName(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.DISABLED_UNSUPPORTED);
}
@Test
public void hasIntentHandler_shouldBeAvailable() {
mPackageManager.addResolveInfoForIntent(TEST_INTENT, new ResolveInfo());
mController.setPackageName(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE);
mController.displayPreference(mScreen);
final Intent intent = mPreference.getIntent();
assertThat(intent.getAction()).isEqualTo(TEST_INTENT.getAction());
assertThat(intent.getStringExtra(EXTRA_PACKAGE_NAME))
.isEqualTo(TEST_INTENT.getStringExtra(EXTRA_PACKAGE_NAME));
}
}