blob: 6e57fe25c2701e186c07c267a9ea065027ffc2d4 [file] [log] [blame]
Sagar Dhawan21a5d392015-07-16 14:31:45 -07001/*
2 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 */
30package com.android.browser;
31
32import android.content.Context;
33import android.content.Intent;
34import android.preference.Preference;
35import android.preference.SwitchPreference;
36import android.provider.Settings;
37import android.util.AttributeSet;
38import android.view.View;
39
40import org.codeaurora.swe.PermissionsServiceFactory;
41
42public class BrowserLocationSwitchPreference extends SwitchPreference {
43
44 View mView;
45 private boolean mSwitchEnabled = true; //internal state tracker
46 private OnPreferenceClickListener onPreferenceClickListener;
47 private OnPreferenceChangeListener oldPreferenceChangeListener;
48
49 public BrowserLocationSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
50 super(context, attrs, defStyleAttr);
51 }
52
53 public BrowserLocationSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
54 super(context, attrs, defStyleAttr, defStyleRes);
55 }
56
57 public BrowserLocationSwitchPreference(Context context, AttributeSet attrs) {
58 super(context, attrs);
59 }
60
61 public BrowserLocationSwitchPreference(Context context) {
62 super(context);
63 }
64
65 @Override
66 protected void onBindView(View view) {
67 super.onBindView(view);
68 mView = view;
69 if (oldPreferenceChangeListener == null){ //set it just the first time
70 oldPreferenceChangeListener = getOnPreferenceChangeListener();
71 }
72 if (mView != null && mSwitchEnabled) {
73 mView.setAlpha((float) 1.0);
74 }
75 else if (mView != null){
76 mView.setAlpha((float) 0.5); //Gray out the option
77 }
78 }
79
80 @Override
81 public void onClick(){
82 //This toggles teh switch
83 if (PermissionsServiceFactory.isSystemLocationEnabled() && mSwitchEnabled) super.onClick();
84 else {
85 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
86 getContext().startActivity(intent);
87 };
88 }
89
90 @Override
91 public void setEnabled(boolean enable){
92 // setEnabled will not call super.setEnabled(enable) because
93 // we want to avoid the default behavior entirely.
94 if (!mSwitchEnabled && enable) { //Transition from off to on
95 if(mView != null)
96 mView.setAlpha((float) 1.0);
97 setOnPreferenceClickListener(onPreferenceClickListener);
98 setOnPreferenceChangeListener(oldPreferenceChangeListener);
99 }
100 else if(!enable && mSwitchEnabled) { //Transition from on to off
101 if (mView != null) {
102 mView.setAlpha((float) 0.5); //Gray out the option
103 }
104 onPreferenceClickListener = getOnPreferenceClickListener();
105 if (oldPreferenceChangeListener == null) //to protect against calling !enable onresume()
106 oldPreferenceChangeListener = getOnPreferenceChangeListener();
107
108 // Prevent clicks from registering.
109 setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
110 @Override
111 public boolean onPreferenceChange(Preference preference, Object newValue) {
112 return false; // Do Not update
113 }
114 });
115 }
116 mSwitchEnabled = enable;
117 }
118}