1 /*
2  * Copyright (C) 2021 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.tv.settings.privacy;
18 
19 import static android.hardware.SensorPrivacyManager.TOGGLE_TYPE_HARDWARE;
20 import static android.hardware.SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE;
21 
22 import android.content.Context;
23 import android.hardware.SensorPrivacyManager;
24 import android.os.Bundle;
25 import android.util.Log;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.ImageView;
30 import android.widget.TextView;
31 
32 import androidx.annotation.Keep;
33 import androidx.annotation.NonNull;
34 
35 import com.android.tv.settings.R;
36 import com.android.tv.twopanelsettings.slices.InfoFragment;
37 
38 
39 /**
40  * A {@link InfoFragment} that hosts the preview pane of the sensor fragment when the toggle is
41  * focused.
42  */
43 @Keep
44 public class SensorToggleInfoFragment extends InfoFragment {
45 
46     private static final String TAG = "SensorToggleInfoFragment";
47     public static final String TOGGLE_EXTRA = "toggle";
48 
49     private TextView mTitleView;
50     private SensorPrivacyManager mSensorPrivacyManager;
51 
52     private PrivacyToggle mToggle;
53 
54     private final SensorPrivacyManager.OnSensorPrivacyChangedListener mPrivacyChangedListener =
55             (sensor, enabled) -> updateTitle();
56 
57     @Override
onAttach(@onNull Context context)58     public void onAttach(@NonNull Context context) {
59         super.onAttach(context);
60         mToggle = (PrivacyToggle) getArguments().get(TOGGLE_EXTRA);
61         if (mToggle == null) {
62             Log.e(TAG, "toggle not set as an extra");
63         }
64     }
65 
66     @Override
onCreate(Bundle savedInstanceState)67     public void onCreate(Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69         mSensorPrivacyManager = (SensorPrivacyManager)
70                 getContext().getSystemService(Context.SENSOR_PRIVACY_SERVICE);
71     }
72 
73     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)74     public View onCreateView(
75             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
76 
77         View view = super.onCreateView(inflater, container, savedInstanceState);
78         ImageView icon = view.findViewById(R.id.info_title_icon);
79         icon.setImageResource(R.drawable.ic_info_outline_base);
80         icon.setVisibility(View.VISIBLE);
81 
82         ((TextView) view.findViewById(R.id.info_summary)).setText(mToggle.toggleInfoText);
83 
84         mTitleView = view.findViewById(R.id.info_title);
85         mTitleView.setVisibility(View.VISIBLE);
86         updateTitle();
87         mSensorPrivacyManager.addSensorPrivacyListener(mToggle.sensor,
88                 mPrivacyChangedListener);
89         return view;
90     }
91 
updateTitle()92     private void updateTitle() {
93         if (mTitleView == null) {
94             return;
95         }
96 
97         boolean softwarePrivacyEnabled = mSensorPrivacyManager.isSensorPrivacyEnabled(
98                 TOGGLE_TYPE_SOFTWARE, mToggle.sensor);
99         boolean physicalPrivacyEnabled = mSensorPrivacyManager.isSensorPrivacyEnabled(
100                 TOGGLE_TYPE_HARDWARE, mToggle.sensor);
101         boolean accessAllowed = !softwarePrivacyEnabled && !physicalPrivacyEnabled;
102         String toggleState = getString(
103                 accessAllowed ? R.string.sensor_toggle_info_on : R.string.sensor_toggle_info_off);
104         mTitleView.setText(getString(mToggle.toggleInfoTitle, toggleState));
105     }
106 
107     @Override
onDestroyView()108     public void onDestroyView() {
109         mSensorPrivacyManager.removeSensorPrivacyListener(mToggle.sensor, mPrivacyChangedListener);
110         mTitleView = null;
111         super.onDestroyView();
112     }
113 }
114