1 /* 2 * Copyright (C) 2018 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 17 package com.android.dialer.common.preference; 18 19 import static android.support.v4.content.ContextCompat.startActivity; 20 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.res.TypedArray; 24 import android.net.Uri; 25 import android.preference.SwitchPreference; 26 import android.util.AttributeSet; 27 import android.view.View; 28 import android.view.View.OnClickListener; 29 import android.view.ViewGroup; 30 import com.android.dialer.common.Assert; 31 32 /** 33 * Utility to allow the summary of a {@link SwitchPreference} to be clicked and opened via a browser 34 * to the specified {@link urlToOpen} attribute while maintaining all other aspects of a {@link 35 * SwitchPreference}. 36 * 37 * <p>Example usage: 38 * 39 * <pre> 40 * <com.android.dialer.common.preference.SwitchPreferenceWithClickableSummary 41 * android:dependency="...." 42 * android:key="...." 43 * android:title="...." 44 * app:urlToOpen="...."/> 45 * </pre> 46 */ 47 public class SwitchPreferenceWithClickableSummary extends SwitchPreference { 48 private final String urlToOpen; 49 SwitchPreferenceWithClickableSummary( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)50 public SwitchPreferenceWithClickableSummary( 51 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 52 super(context, attrs, defStyleAttr, defStyleRes); 53 TypedArray typedArray = 54 context.obtainStyledAttributes(attrs, R.styleable.SwitchPreferenceWithClickableSummary); 55 urlToOpen = 56 String.valueOf( 57 typedArray.getText(R.styleable.SwitchPreferenceWithClickableSummary_urlToOpen)); 58 } 59 SwitchPreferenceWithClickableSummary( Context context, AttributeSet attrs, int defStyleAttr)60 public SwitchPreferenceWithClickableSummary( 61 Context context, AttributeSet attrs, int defStyleAttr) { 62 super(context, attrs, defStyleAttr, defStyleAttr); 63 TypedArray typedArray = 64 context.obtainStyledAttributes(attrs, R.styleable.SwitchPreferenceWithClickableSummary); 65 urlToOpen = 66 String.valueOf( 67 typedArray.getText(R.styleable.SwitchPreferenceWithClickableSummary_urlToOpen)); 68 } 69 SwitchPreferenceWithClickableSummary(Context context, AttributeSet attrs)70 public SwitchPreferenceWithClickableSummary(Context context, AttributeSet attrs) { 71 super(context, attrs); 72 TypedArray typedArray = 73 context.obtainStyledAttributes(attrs, R.styleable.SwitchPreferenceWithClickableSummary); 74 urlToOpen = 75 String.valueOf( 76 typedArray.getText(R.styleable.SwitchPreferenceWithClickableSummary_urlToOpen)); 77 } 78 SwitchPreferenceWithClickableSummary(Context context)79 public SwitchPreferenceWithClickableSummary(Context context) { 80 this(context, null); 81 } 82 83 @Override onCreateView(ViewGroup parent)84 protected View onCreateView(ViewGroup parent) { 85 return super.onCreateView(parent); 86 } 87 88 @Override onBindView(View view)89 protected void onBindView(View view) { 90 super.onBindView(view); 91 Assert.checkArgument( 92 urlToOpen != null, 93 "must have a urlToOpen attribute when using SwitchPreferenceWithClickableSummary"); 94 view.findViewById(android.R.id.summary) 95 .setOnClickListener( 96 new OnClickListener() { 97 @Override 98 public void onClick(View v) { 99 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlToOpen)); 100 startActivity(view.getContext(), intent, null); 101 } 102 }); 103 } 104 } 105