Add entry point to launch media output slice

Add media output panel type to launch media output slice.

Bug: 121083246
Test: make -j RunSettingsRoboTests
Change-Id: Ibf706146430e309fef6cbf0e1e86c2d5b78b50d5
This commit is contained in:
hughchen
2018-12-19 19:17:32 +08:00
parent e56df8877a
commit 2177813531
13 changed files with 288 additions and 16 deletions

View File

@@ -2983,6 +2983,17 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity-alias
android:name="MediaOutputSlice"
android:label="@string/media_output_panel_title"
android:permission="android.permission.BLUETOOTH_PRIVILEGED"
android:targetActivity=".panel.SettingsPanelActivity">
<intent-filter>
<action android:name="com.android.settings.panel.action.MEDIA_OUTPUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
<provider android:name=".slices.SettingsSliceProvider" <provider android:name=".slices.SettingsSliceProvider"
android:authorities="com.android.settings.slices;android.settings.slices" android:authorities="com.android.settings.slices;android.settings.slices"
android:exported="true" android:exported="true"

View File

@@ -0,0 +1,74 @@
/*
* 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.settings.panel;
import static com.android.settings.media.MediaOutputSlice.MEDIA_PACKAGE_NAME;
import static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_SLICE_URI;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import com.android.settings.R;
import java.util.ArrayList;
import java.util.List;
/**
* Represents the Media output Panel.
*
* <p>
* Displays Media output item
* </p>
*/
public class MediaOutputPanel implements PanelContent {
private final Context mContext;
private final String mPackageName;
public static MediaOutputPanel create(Context context, String packageName) {
return new MediaOutputPanel(context, packageName);
}
private MediaOutputPanel(Context context, String packageName) {
mContext = context.getApplicationContext();
mPackageName = packageName;
}
@Override
public CharSequence getTitle() {
return mContext.getText(R.string.media_output_panel_title);
}
@Override
public List<Uri> getSlices() {
final List<Uri> uris = new ArrayList<>();
MEDIA_OUTPUT_SLICE_URI =
MEDIA_OUTPUT_SLICE_URI
.buildUpon()
.clearQuery()
.appendQueryParameter(MEDIA_PACKAGE_NAME, mPackageName)
.build();
uris.add(MEDIA_OUTPUT_SLICE_URI);
return uris;
}
@Override
public Intent getSeeMoreIntent() {
return null;
}
}

View File

@@ -21,7 +21,7 @@ import android.content.Context;
public interface PanelFeatureProvider { public interface PanelFeatureProvider {
/** /**
* Returns {@link PanelContent} as specified by the {@param panelType}. * Returns {@link PanelContent} as specified by the {@code panelType} and {@code packageName}.
*/ */
PanelContent getPanel(Context context, String panelType); PanelContent getPanel(Context context, String panelType, String packageName);
} }

View File

@@ -16,13 +16,15 @@
package com.android.settings.panel; package com.android.settings.panel;
import static com.android.settingslib.media.MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
public class PanelFeatureProviderImpl implements PanelFeatureProvider { public class PanelFeatureProviderImpl implements PanelFeatureProvider {
@Override @Override
public PanelContent getPanel(Context context, String panelType) { public PanelContent getPanel(Context context, String panelType, String packageName) {
switch (panelType) { switch (panelType) {
case Settings.Panel.ACTION_INTERNET_CONNECTIVITY: case Settings.Panel.ACTION_INTERNET_CONNECTIVITY:
return InternetConnectivityPanel.create(context); return InternetConnectivityPanel.create(context);
@@ -30,6 +32,8 @@ public class PanelFeatureProviderImpl implements PanelFeatureProvider {
return VolumePanel.create(context); return VolumePanel.create(context);
case Settings.Panel.ACTION_NFC: case Settings.Panel.ACTION_NFC:
return NfcPanel.create(context); return NfcPanel.create(context);
case ACTION_MEDIA_OUTPUT:
return MediaOutputPanel.create(context, packageName);
} }
throw new IllegalStateException("No matching panel for: " + panelType); throw new IllegalStateException("No matching panel for: " + panelType);

View File

@@ -70,10 +70,12 @@ public class PanelFragment extends Fragment {
final Bundle arguments = getArguments(); final Bundle arguments = getArguments();
final String panelType = arguments.getString(SettingsPanelActivity.KEY_PANEL_TYPE_ARGUMENT); final String panelType = arguments.getString(SettingsPanelActivity.KEY_PANEL_TYPE_ARGUMENT);
final String packageName =
arguments.getString(SettingsPanelActivity.KEY_PANEL_PACKAGE_NAME);
final PanelContent panel = FeatureFactory.getFactory(activity) final PanelContent panel = FeatureFactory.getFactory(activity)
.getPanelFeatureProvider() .getPanelFeatureProvider()
.getPanel(activity, panelType); .getPanel(activity, panelType, packageName);
mAdapter = new PanelSlicesAdapter(this, panel.getSlices()); mAdapter = new PanelSlicesAdapter(this, panel.getSlices());
@@ -86,6 +88,11 @@ public class PanelFragment extends Fragment {
mSeeMoreButton.setOnClickListener(getSeeMoreListener(panel.getSeeMoreIntent())); mSeeMoreButton.setOnClickListener(getSeeMoreListener(panel.getSeeMoreIntent()));
mDoneButton.setOnClickListener(mDoneButtonListener); mDoneButton.setOnClickListener(mDoneButtonListener);
//If getSeeMoreIntent() is null, hide the mSeeMoreButton.
if (panel.getSeeMoreIntent() == null) {
mSeeMoreButton.setVisibility(View.GONE);
}
return view; return view;
} }

View File

@@ -16,8 +16,12 @@
package com.android.settings.panel; package com.android.settings.panel;
import static com.android.settingslib.media.MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT;
import static com.android.settingslib.media.MediaOutputSliceConstants.EXTRA_PACKAGE_NAME;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.Gravity; import android.view.Gravity;
import android.view.Window; import android.view.Window;
@@ -28,6 +32,7 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R; import com.android.settings.R;
/** /**
@@ -37,10 +42,14 @@ public class SettingsPanelActivity extends FragmentActivity {
private final String TAG = "panel_activity"; private final String TAG = "panel_activity";
@VisibleForTesting
final Bundle mBundle = new Bundle();
/** /**
* Key specifying which Panel the app is requesting. * Key specifying which Panel the app is requesting.
*/ */
public static final String KEY_PANEL_TYPE_ARGUMENT = "PANEL_TYPE_ARGUMENT"; public static final String KEY_PANEL_TYPE_ARGUMENT = "PANEL_TYPE_ARGUMENT";
public static final String KEY_PANEL_PACKAGE_NAME = "PANEL_PACKAGE_NAME";
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -53,6 +62,16 @@ public class SettingsPanelActivity extends FragmentActivity {
return; return;
} }
final String packageName =
callingIntent.getStringExtra(EXTRA_PACKAGE_NAME);
if (TextUtils.equals(ACTION_MEDIA_OUTPUT, callingIntent.getAction())
&& TextUtils.isEmpty(packageName)) {
Log.e(TAG, "Null package name, closing Panel Activity");
finish();
return;
}
setContentView(R.layout.settings_panel); setContentView(R.layout.settings_panel);
// Move the window to the bottom of screen, and make it take up the entire screen width. // Move the window to the bottom of screen, and make it take up the entire screen width.
@@ -61,11 +80,11 @@ public class SettingsPanelActivity extends FragmentActivity {
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT); WindowManager.LayoutParams.WRAP_CONTENT);
final Bundle bundle = new Bundle(); mBundle.putString(KEY_PANEL_TYPE_ARGUMENT, callingIntent.getAction());
bundle.putString(KEY_PANEL_TYPE_ARGUMENT, callingIntent.getAction()); mBundle.putString(KEY_PANEL_PACKAGE_NAME, packageName);
final PanelFragment panelFragment = new PanelFragment(); final PanelFragment panelFragment = new PanelFragment();
panelFragment.setArguments(bundle); panelFragment.setArguments(mBundle);
final FragmentManager fragmentManager = getSupportFragmentManager(); final FragmentManager fragmentManager = getSupportFragmentManager();
final Fragment fragment = fragmentManager.findFragmentById(R.id.main_content); final Fragment fragment = fragmentManager.findFragmentById(R.id.main_content);

View File

@@ -18,6 +18,7 @@ package com.android.settings.slices;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.text.TextUtils;
import android.util.ArrayMap; import android.util.ArrayMap;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
@@ -33,6 +34,7 @@ import com.android.settings.homepage.contextualcards.slices.BluetoothDevicesSlic
import com.android.settings.homepage.contextualcards.slices.LowStorageSlice; import com.android.settings.homepage.contextualcards.slices.LowStorageSlice;
import com.android.settings.homepage.contextualcards.slices.NotificationChannelSlice; import com.android.settings.homepage.contextualcards.slices.NotificationChannelSlice;
import com.android.settings.location.LocationSlice; import com.android.settings.location.LocationSlice;
import com.android.settings.media.MediaOutputSlice;
import com.android.settings.wifi.slice.ContextualWifiSlice; import com.android.settings.wifi.slice.ContextualWifiSlice;
import com.android.settings.wifi.slice.WifiSlice; import com.android.settings.wifi.slice.WifiSlice;
@@ -65,20 +67,25 @@ public class CustomSliceManager {
* the only thing that should be needed to create the object. * the only thing that should be needed to create the object.
*/ */
public CustomSliceable getSliceableFromUri(Uri uri) { public CustomSliceable getSliceableFromUri(Uri uri) {
if (mSliceableCache.containsKey(uri)) { final Uri newUri = removeParameterFromUri(uri);
return mSliceableCache.get(uri); if (mSliceableCache.containsKey(newUri)) {
return mSliceableCache.get(newUri);
} }
final Class clazz = mUriMap.get(uri); final Class clazz = mUriMap.get(newUri);
if (clazz == null) { if (clazz == null) {
throw new IllegalArgumentException("No Slice found for uri: " + uri); throw new IllegalArgumentException("No Slice found for uri: " + uri);
} }
final CustomSliceable sliceable = CustomSliceable.createInstance(mContext, clazz); final CustomSliceable sliceable = CustomSliceable.createInstance(mContext, clazz);
mSliceableCache.put(uri, sliceable); mSliceableCache.put(newUri, sliceable);
return sliceable; return sliceable;
} }
private Uri removeParameterFromUri(Uri uri) {
return uri != null ? uri.buildUpon().clearQuery().build() : null;
}
/** /**
* Return a {@link CustomSliceable} associated to the Action. * Return a {@link CustomSliceable} associated to the Action.
* <p> * <p>
@@ -94,7 +101,7 @@ public class CustomSliceManager {
* {@link CustomSliceManager}. * {@link CustomSliceManager}.
*/ */
public boolean isValidUri(Uri uri) { public boolean isValidUri(Uri uri) {
return mUriMap.containsKey(uri); return mUriMap.containsKey(removeParameterFromUri(uri));
} }
/** /**
@@ -120,5 +127,6 @@ public class CustomSliceManager {
NotificationChannelSlice.class); NotificationChannelSlice.class);
mUriMap.put(CustomSliceRegistry.STORAGE_SLICE_URI, StorageSlice.class); mUriMap.put(CustomSliceRegistry.STORAGE_SLICE_URI, StorageSlice.class);
mUriMap.put(CustomSliceRegistry.WIFI_SLICE_URI, WifiSlice.class); mUriMap.put(CustomSliceRegistry.WIFI_SLICE_URI, WifiSlice.class);
mUriMap.put(CustomSliceRegistry.MEDIA_OUTPUT_SLICE_URI, MediaOutputSlice.class);
} }
} }

View File

@@ -27,6 +27,7 @@ import android.provider.SettingsSlicesContract;
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController; import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
import com.android.settings.wifi.calling.WifiCallingSliceHelper; import com.android.settings.wifi.calling.WifiCallingSliceHelper;
import com.android.settingslib.media.MediaOutputSliceConstants;
/** /**
* A registry of custom slice Uris. * A registry of custom slice Uris.
@@ -255,4 +256,14 @@ public class CustomSliceRegistry {
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION) .appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
.appendPath(ZEN_MODE_KEY) .appendPath(ZEN_MODE_KEY)
.build(); .build();
/**
* Backing Uri for the Media output Slice.
*/
public static Uri MEDIA_OUTPUT_SLICE_URI = new Uri.Builder()
.scheme(ContentResolver.SCHEME_CONTENT)
.authority(SettingsSliceProvider.SLICE_AUTHORITY)
.appendPath(SettingsSlicesContract.PATH_SETTING_ACTION)
.appendPath(MediaOutputSliceConstants.KEY_MEDIA_OUTPUT)
.build();
} }

View File

@@ -0,0 +1,65 @@
/*
* 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.settings.panel;
import static com.android.settings.media.MediaOutputSlice.MEDIA_PACKAGE_NAME;
import static com.google.common.truth.Truth.assertThat;
import android.net.Uri;
import com.android.settings.slices.CustomSliceRegistry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class MediaOutputPanelTest {
private static final String TEST_PACKAGENAME = "com.test.packagename";
private MediaOutputPanel mPanel;
@Before
public void setUp() {
mPanel = MediaOutputPanel.create(RuntimeEnvironment.application, TEST_PACKAGENAME);
}
@Test
public void getSlices_containsNecessarySlices() {
final List<Uri> uris = mPanel.getSlices();
assertThat(uris).containsExactly(CustomSliceRegistry.MEDIA_OUTPUT_SLICE_URI);
}
@Test
public void getSlices_verifyPackageName_isEqual() {
final List<Uri> uris = mPanel.getSlices();
assertThat(uris.get(0).getQueryParameter(MEDIA_PACKAGE_NAME)).isEqualTo(TEST_PACKAGENAME);
}
@Test
public void getSeeMoreIntent_isNull() {
assertThat(mPanel.getSeeMoreIntent()).isNull();
}
}

View File

@@ -17,6 +17,8 @@
package com.android.settings.panel; package com.android.settings.panel;
import static com.android.settingslib.media.MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.content.Context; import android.content.Context;
@@ -32,6 +34,8 @@ import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class PanelFeatureProviderImplTest { public class PanelFeatureProviderImplTest {
private static final String TEST_PACKAGENAME = "com.test.packagename";
private Context mContext; private Context mContext;
private PanelFeatureProviderImpl mProvider; private PanelFeatureProviderImpl mProvider;
@@ -44,7 +48,7 @@ public class PanelFeatureProviderImplTest {
@Test @Test
public void getPanel_internetConnectivityKey_returnsCorrectPanel() { public void getPanel_internetConnectivityKey_returnsCorrectPanel() {
final PanelContent panel = mProvider.getPanel(mContext, final PanelContent panel = mProvider.getPanel(mContext,
Settings.Panel.ACTION_INTERNET_CONNECTIVITY); Settings.Panel.ACTION_INTERNET_CONNECTIVITY, TEST_PACKAGENAME);
assertThat(panel).isInstanceOf(InternetConnectivityPanel.class); assertThat(panel).isInstanceOf(InternetConnectivityPanel.class);
} }
@@ -52,8 +56,16 @@ public class PanelFeatureProviderImplTest {
@Test @Test
public void getPanel_volume_returnsCorrectPanel() { public void getPanel_volume_returnsCorrectPanel() {
final PanelContent panel = mProvider.getPanel(mContext, final PanelContent panel = mProvider.getPanel(mContext,
Settings.Panel.ACTION_VOLUME); Settings.Panel.ACTION_VOLUME, TEST_PACKAGENAME);
assertThat(panel).isInstanceOf(VolumePanel.class); assertThat(panel).isInstanceOf(VolumePanel.class);
} }
@Test
public void getPanel_mediaOutputKey_returnsCorrectPanel() {
final PanelContent panel = mProvider.getPanel(mContext,
ACTION_MEDIA_OUTPUT, TEST_PACKAGENAME);
assertThat(panel).isInstanceOf(MediaOutputPanel.class);
}
} }

View File

@@ -59,7 +59,7 @@ public class PanelFragmentTest {
mFakeFeatureFactory = FakeFeatureFactory.setupForTest(); mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
mFakeFeatureFactory.panelFeatureProvider = mPanelFeatureProvider; mFakeFeatureFactory.panelFeatureProvider = mPanelFeatureProvider;
mFakePanelContent = new FakePanelContent(); mFakePanelContent = new FakePanelContent();
doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any()); doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any(), any());
ActivityController<FakeSettingsPanelActivity> activityController = ActivityController<FakeSettingsPanelActivity> activityController =
Robolectric.buildActivity(FakeSettingsPanelActivity.class); Robolectric.buildActivity(FakeSettingsPanelActivity.class);

View File

@@ -58,7 +58,7 @@ public class PanelSlicesAdapterTest {
mFakeFeatureFactory = FakeFeatureFactory.setupForTest(); mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
mFakeFeatureFactory.panelFeatureProvider = mPanelFeatureProvider; mFakeFeatureFactory.panelFeatureProvider = mPanelFeatureProvider;
mFakePanelContent = new FakePanelContent(); mFakePanelContent = new FakePanelContent();
doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any()); doReturn(mFakePanelContent).when(mPanelFeatureProvider).getPanel(any(), any(), any());
ActivityController<FakeSettingsPanelActivity> activityController = ActivityController<FakeSettingsPanelActivity> activityController =
Robolectric.buildActivity(FakeSettingsPanelActivity.class); Robolectric.buildActivity(FakeSettingsPanelActivity.class);

View File

@@ -0,0 +1,61 @@
/*
* 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.settings.panel;
import static com.android.settings.panel.SettingsPanelActivity.KEY_PANEL_PACKAGE_NAME;
import static com.android.settings.panel.SettingsPanelActivity.KEY_PANEL_TYPE_ARGUMENT;
import static com.google.common.truth.Truth.assertThat;
import android.content.Intent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class SettingsPanelActivityTest {
@Test
public void startMediaOutputSlice_withPackageName_bundleShouldHaveValue() {
final Intent intent = new Intent()
.setAction("com.android.settings.panel.action.MEDIA_OUTPUT")
.putExtra("com.android.settings.panel.extra.PACKAGE_NAME",
"com.google.android.music");
final SettingsPanelActivity activity =
Robolectric.buildActivity(SettingsPanelActivity.class, intent).create().get();
assertThat(activity.mBundle.getString(KEY_PANEL_PACKAGE_NAME))
.isEqualTo("com.google.android.music");
assertThat(activity.mBundle.getString(KEY_PANEL_TYPE_ARGUMENT))
.isEqualTo("com.android.settings.panel.action.MEDIA_OUTPUT");
}
@Test
public void startMediaOutputSlice_withoutPackageName_bundleShouldNotHaveValue() {
final Intent intent = new Intent()
.setAction("com.android.settings.panel.action.MEDIA_OUTPUT");
final SettingsPanelActivity activity =
Robolectric.buildActivity(SettingsPanelActivity.class, intent).create().get();
assertThat(activity.mBundle.containsKey(KEY_PANEL_PACKAGE_NAME)).isFalse();
assertThat(activity.mBundle.containsKey(KEY_PANEL_TYPE_ARGUMENT)).isFalse();
}
}