Home | History | Annotate | Download | only in apps
      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 
     17 package com.android.tv.settings.device.apps;
     18 
     19 import android.app.INotificationManager;
     20 import android.app.NotificationManager;
     21 import android.content.Context;
     22 import android.os.RemoteException;
     23 import android.support.annotation.NonNull;
     24 import android.support.v14.preference.SwitchPreference;
     25 import android.util.Log;
     26 
     27 import com.android.settingslib.applications.ApplicationsState;
     28 import com.android.tv.settings.R;
     29 
     30 public class NotificationsPreference extends SwitchPreference {
     31     private static final String TAG = "NotificationsPreference";
     32 
     33     private final INotificationManager mNotificationManager;
     34     private ApplicationsState.AppEntry mEntry;
     35 
     36     public NotificationsPreference(Context context, ApplicationsState.AppEntry entry) {
     37         super(context);
     38         mEntry = entry;
     39 
     40         mNotificationManager = NotificationManager.getService();
     41 
     42         refresh();
     43     }
     44 
     45     /**
     46      * Set entry and refresh pref.
     47      * @param entry entry
     48      */
     49     public void setEntry(@NonNull ApplicationsState.AppEntry entry) {
     50         mEntry = entry;
     51         refresh();
     52     }
     53 
     54     public void refresh() {
     55         setTitle(R.string.device_apps_app_management_notifications);
     56 
     57         try {
     58             super.setChecked(
     59                     mNotificationManager.areNotificationsEnabledForPackage(mEntry.info.packageName,
     60                             mEntry.info.uid));
     61         } catch (RemoteException e) {
     62             Log.d(TAG, "Remote exception while checking notifications for package "
     63                     + mEntry.info.packageName, e);
     64         }
     65     }
     66 
     67     @Override
     68     public void setChecked(boolean checked) {
     69         if (setNotificationsEnabled(checked)) {
     70             super.setChecked(checked);
     71         }
     72     }
     73 
     74     private boolean setNotificationsEnabled(boolean enabled) {
     75         boolean result = true;
     76         if (isChecked() != enabled) {
     77             try {
     78                 mNotificationManager.setNotificationsEnabledForPackage(
     79                         mEntry.info.packageName, mEntry.info.uid, enabled);
     80             } catch (android.os.RemoteException ex) {
     81                 result = false;
     82             }
     83         }
     84         return result;
     85     }
     86 
     87 }
     88