Implement the Copyable interface to PhoneNumberPreferenceController
Change-Id: I70085cffe13047ac808d1bf08a467f693b1d8737 Fixes: 118398321 Test: manual and robotests
This commit is contained in:
@@ -10343,4 +10343,7 @@
|
||||
<string name="contextual_card_feedback_send">Send feedback</string>
|
||||
<!-- String for contextual card feedback dialog [CHAR LIMIT=NONE] -->
|
||||
<string name="contextual_card_feedback_confirm_message">Would you like to give us feedback on this suggestion?</string>
|
||||
|
||||
<!-- Toast message for copy action of Copyable Slice [CHAR LIMIT=NONE] -->
|
||||
<string name="copyable_slice_toast"><xliff:g id="copy_content" example="Phone number">%1$s</xliff:g> copied to clipboard.</string>
|
||||
</resources>
|
||||
|
@@ -16,6 +16,10 @@
|
||||
|
||||
package com.android.settings.deviceinfo;
|
||||
|
||||
import static android.content.Context.CLIPBOARD_SERVICE;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
@@ -23,6 +27,7 @@ import android.telephony.TelephonyManager;
|
||||
import android.text.BidiFormatter;
|
||||
import android.text.TextDirectionHeuristics;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
@@ -30,12 +35,14 @@ import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.slices.Copyable;
|
||||
import com.android.settingslib.DeviceInfoUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PhoneNumberPreferenceController extends BasePreferenceController {
|
||||
public class PhoneNumberPreferenceController extends BasePreferenceController implements
|
||||
Copyable {
|
||||
|
||||
private final static String KEY_PHONE_NUMBER = "phone_number";
|
||||
|
||||
@@ -91,6 +98,17 @@ public class PhoneNumberPreferenceController extends BasePreferenceController {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copy() {
|
||||
final ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(
|
||||
CLIPBOARD_SERVICE);
|
||||
clipboard.setPrimaryClip(ClipData.newPlainText("text", getFirstPhoneNumber()));
|
||||
|
||||
final String toast = mContext.getString(R.string.copyable_slice_toast,
|
||||
mContext.getText(R.string.status_number));
|
||||
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private CharSequence getFirstPhoneNumber() {
|
||||
final List<SubscriptionInfo> subscriptionInfoList =
|
||||
mSubscriptionManager.getActiveSubscriptionInfoList();
|
||||
|
@@ -19,7 +19,7 @@ package com.android.settings.slices;
|
||||
/**
|
||||
* Provide the copy ability for preference controller to copy the data to the clipboard.
|
||||
*/
|
||||
public interface CopyableSlice {
|
||||
public interface Copyable {
|
||||
/**
|
||||
* Copy the key slice information to the clipboard.
|
||||
* It is highly recommended to show the toast to notify users when implemented this function.
|
@@ -185,7 +185,7 @@ public class SliceBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
final BasePreferenceController controller = getPreferenceController(context, key);
|
||||
|
||||
if (!(controller instanceof CopyableSlice)) {
|
||||
if (!(controller instanceof Copyable)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Copyable action passed for a non-copyable key:" + key);
|
||||
}
|
||||
@@ -198,7 +198,7 @@ public class SliceBroadcastReceiver extends BroadcastReceiver {
|
||||
return;
|
||||
}
|
||||
|
||||
((CopyableSlice) controller).copy();
|
||||
((Copyable) controller).copy();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -94,7 +94,7 @@ public class SliceBuilderUtils {
|
||||
return buildUnavailableSlice(context, sliceData);
|
||||
}
|
||||
|
||||
if (controller instanceof CopyableSlice) {
|
||||
if (controller instanceof Copyable) {
|
||||
return buildCopyableSlice(context, sliceData, controller);
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package com.android.settings.deviceinfo;
|
||||
|
||||
import static android.content.Context.CLIPBOARD_SERVICE;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
@@ -24,6 +26,7 @@ import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.telephony.SubscriptionInfo;
|
||||
import android.telephony.SubscriptionManager;
|
||||
@@ -43,6 +46,9 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class PhoneNumberPreferenceControllerTest {
|
||||
|
||||
@@ -146,4 +152,20 @@ public class PhoneNumberPreferenceControllerTest {
|
||||
public void isSliceable_shouldBeTrue() {
|
||||
assertThat(mController.isSliceable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copy_shouldCopyPhoneNumberToClipboard() {
|
||||
final List<SubscriptionInfo> list = new ArrayList<>();
|
||||
list.add(mSubscriptionInfo);
|
||||
when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(list);
|
||||
final String phoneNumber = "1111111111";
|
||||
doReturn(phoneNumber).when(mController).getFormattedPhoneNumber(mSubscriptionInfo);
|
||||
|
||||
mController.copy();
|
||||
|
||||
final ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(
|
||||
CLIPBOARD_SERVICE);
|
||||
final CharSequence data = clipboard.getPrimaryClip().getItemAt(0).getText();
|
||||
assertThat(phoneNumber.contentEquals(data)).isTrue();
|
||||
}
|
||||
}
|
||||
|
@@ -19,9 +19,9 @@ package com.android.settings.testutils;
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settings.slices.CopyableSlice;
|
||||
import com.android.settings.slices.Copyable;
|
||||
|
||||
public class FakeCopyableController extends BasePreferenceController implements CopyableSlice {
|
||||
public class FakeCopyableController extends BasePreferenceController implements Copyable {
|
||||
|
||||
public FakeCopyableController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
|
Reference in New Issue
Block a user