Loading res/xml/app_info_settings.xml +14 −9 Original line number Diff line number Diff line Loading @@ -62,6 +62,11 @@ android:title="@string/data_usage_summary_title" 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" Loading src/com/android/settings/applications/appinfo/AppInfoDashboardFragment.java +10 −8 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -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()) { Loading Loading @@ -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; Loading src/com/android/settings/applications/appinfo/TimeSpentInAppPreferenceController.java 0 → 100644 +75 −0 Original line number Diff line number Diff line /* * 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); } } } src/com/android/settings/core/PreferenceControllerListHelper.java +1 −1 Original line number Diff line number Diff line Loading @@ -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; } Loading tests/robotests/src/com/android/settings/applications/appinfo/TimeSpentInAppPreferenceControllerTest.java 0 → 100644 +98 −0 Original line number Diff line number Diff line /* * 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)); } } Loading
res/xml/app_info_settings.xml +14 −9 Original line number Diff line number Diff line Loading @@ -62,6 +62,11 @@ android:title="@string/data_usage_summary_title" 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" Loading
src/com/android/settings/applications/appinfo/AppInfoDashboardFragment.java +10 −8 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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); Loading Loading @@ -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()) { Loading Loading @@ -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; Loading
src/com/android/settings/applications/appinfo/TimeSpentInAppPreferenceController.java 0 → 100644 +75 −0 Original line number Diff line number Diff line /* * 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); } } }
src/com/android/settings/core/PreferenceControllerListHelper.java +1 −1 Original line number Diff line number Diff line Loading @@ -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; } Loading
tests/robotests/src/com/android/settings/applications/appinfo/TimeSpentInAppPreferenceControllerTest.java 0 → 100644 +98 −0 Original line number Diff line number Diff line /* * 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)); } }