1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings; 17 18 import android.content.ClipboardManager; 19 import android.content.Context; 20 import android.support.v7.preference.Preference; 21 import android.support.v7.preference.PreferenceViewHolder; 22 import android.util.AttributeSet; 23 import android.view.View; 24 import android.view.View.OnLongClickListener; 25 import android.widget.Toast; 26 27 public class CopyablePreference extends Preference { 28 CopyablePreference(Context context, AttributeSet attrs)29 public CopyablePreference(Context context, AttributeSet attrs) { 30 super(context, attrs); 31 } 32 CopyablePreference(Context context)33 public CopyablePreference(Context context) { 34 this(context, null); 35 } 36 37 @Override onBindViewHolder(PreferenceViewHolder holder)38 public void onBindViewHolder(PreferenceViewHolder holder) { 39 super.onBindViewHolder(holder); 40 holder.setDividerAllowedAbove(true); 41 holder.setDividerAllowedBelow(true); 42 holder.itemView.setLongClickable(true); 43 holder.itemView.setOnLongClickListener(new OnLongClickListener() { 44 @Override 45 public boolean onLongClick(View v) { 46 copyPreference(getContext(), CopyablePreference.this); 47 return true; 48 } 49 }); 50 } 51 getCopyableText()52 public CharSequence getCopyableText() { 53 return getSummary(); 54 } 55 copyPreference(Context context, CopyablePreference pref)56 public static void copyPreference(Context context, CopyablePreference pref) { 57 ClipboardManager cm = 58 (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); 59 cm.setText(pref.getCopyableText()); 60 Toast.makeText(context, com.android.internal.R.string.text_copied, Toast.LENGTH_SHORT) 61 .show(); 62 } 63 } 64