Home | History | Annotate | Download | only in tuner
      1 /**
      2  * Copyright (c) 2016, 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.systemui.tuner;
     17 
     18 import com.android.internal.logging.MetricsLogger;
     19 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     20 import com.android.systemui.R;
     21 
     22 import android.annotation.Nullable;
     23 import android.app.Fragment;
     24 import android.os.Bundle;
     25 import android.provider.Settings;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.Switch;
     30 import android.widget.TextView;
     31 
     32 public class PowerNotificationControlsFragment extends Fragment {
     33 
     34     private static final String KEY_SHOW_PNC = "show_importance_slider";
     35 
     36     @Override
     37     public void onCreate(@Nullable Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39     }
     40 
     41     @Override
     42     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     43             Bundle savedInstanceState) {
     44         return inflater.inflate(R.layout.power_notification_controls_settings, container, false);
     45     }
     46 
     47     @Override
     48     public void onViewCreated(View view, Bundle savedInstanceState) {
     49         super.onViewCreated(view, savedInstanceState);
     50         final View switchBar = view.findViewById(R.id.switch_bar);
     51         final Switch switchWidget = (Switch) switchBar.findViewById(android.R.id.switch_widget);
     52         final TextView switchText = (TextView) switchBar.findViewById(R.id.switch_text);
     53         switchWidget.setChecked(isEnabled());
     54         switchText.setText(isEnabled()
     55                 ? getString(R.string.switch_bar_on)
     56                 : getString(R.string.switch_bar_off));
     57 
     58         switchWidget.setOnClickListener(new View.OnClickListener() {
     59             @Override
     60             public void onClick(View v) {
     61                 boolean newState = !isEnabled();
     62                 MetricsLogger.action(getContext(),
     63                         MetricsEvent.ACTION_TUNER_POWER_NOTIFICATION_CONTROLS, newState);
     64                 Settings.Secure.putInt(getContext().getContentResolver(),
     65                         KEY_SHOW_PNC, newState ? 1 : 0);
     66                 switchWidget.setChecked(newState);
     67                 switchText.setText(newState
     68                         ? getString(R.string.switch_bar_on)
     69                         : getString(R.string.switch_bar_off));
     70             }
     71         });
     72     }
     73 
     74     @Override
     75     public void onResume() {
     76         super.onResume();
     77         MetricsLogger.visibility(
     78                 getContext(), MetricsEvent.TUNER_POWER_NOTIFICATION_CONTROLS, true);
     79     }
     80 
     81     @Override
     82     public void onPause() {
     83         super.onPause();
     84         MetricsLogger.visibility(
     85                 getContext(), MetricsEvent.TUNER_POWER_NOTIFICATION_CONTROLS, false);
     86     }
     87 
     88     private boolean isEnabled() {
     89         int setting = Settings.Secure.getInt(getContext().getContentResolver(), KEY_SHOW_PNC, 0);
     90         return setting == 1;
     91     }
     92 
     93 }
     94