Home | History | Annotate | Download | only in wifi
      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.server.wifi;
     18 
     19 import android.app.Notification;
     20 import android.app.PendingIntent;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 
     24 import com.android.internal.R;
     25 import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
     26 import com.android.internal.notification.SystemNotificationChannels;
     27 
     28 
     29 /** Factory for Wifi Wake notifications. */
     30 public class WakeupNotificationFactory {
     31 
     32     public static final String ACTION_DISMISS_NOTIFICATION =
     33             "com.android.server.wifi.wakeup.DISMISS_NOTIFICATION";
     34     public static final String ACTION_OPEN_WIFI_PREFERENCES =
     35             "com.android.server.wifi.wakeup.OPEN_WIFI_PREFERENCES";
     36     public static final String ACTION_OPEN_WIFI_SETTINGS =
     37             "com.android.server.wifi.wakeup.OPEN_WIFI_SETTINGS";
     38     public static final String ACTION_TURN_OFF_WIFI_WAKE =
     39             "com.android.server.wifi.wakeup.TURN_OFF_WIFI_WAKE";
     40 
     41     /** Notification channel ID for onboarding messages. */
     42     public static final int ONBOARD_ID = SystemMessage.NOTE_WIFI_WAKE_ONBOARD;
     43 
     44     private final Context mContext;
     45     private final FrameworkFacade mFrameworkFacade;
     46 
     47     WakeupNotificationFactory(Context context, FrameworkFacade frameworkFacade) {
     48         mContext = context;
     49         mFrameworkFacade = frameworkFacade;
     50     }
     51 
     52     /**
     53      * Creates a Wifi Wake onboarding notification.
     54      */
     55     public Notification createOnboardingNotification() {
     56         CharSequence title = mContext.getText(R.string.wifi_wakeup_onboarding_title);
     57         CharSequence content = mContext.getText(R.string.wifi_wakeup_onboarding_subtext);
     58         CharSequence disableText = mContext.getText(R.string.wifi_wakeup_onboarding_action_disable);
     59         int color = mContext.getResources()
     60                 .getColor(R.color.system_notification_accent_color, mContext.getTheme());
     61 
     62         final Notification.Action disableAction = new Notification.Action.Builder(
     63                 null /* icon */, disableText, getPrivateBroadcast(ACTION_TURN_OFF_WIFI_WAKE))
     64                 .build();
     65 
     66         return mFrameworkFacade.makeNotificationBuilder(mContext,
     67                 SystemNotificationChannels.NETWORK_STATUS)
     68                 .setSmallIcon(R.drawable.ic_wifi_settings)
     69                 .setTicker(title)
     70                 .setContentTitle(title)
     71                 .setContentText(content)
     72                 .setContentIntent(getPrivateBroadcast(ACTION_OPEN_WIFI_PREFERENCES))
     73                 .setDeleteIntent(getPrivateBroadcast(ACTION_DISMISS_NOTIFICATION))
     74                 .addAction(disableAction)
     75                 .setShowWhen(false)
     76                 .setLocalOnly(true)
     77                 .setColor(color)
     78                 .build();
     79     }
     80 
     81 
     82     private PendingIntent getPrivateBroadcast(String action) {
     83         Intent intent = new Intent(action).setPackage("android");
     84         return mFrameworkFacade.getBroadcast(
     85                 mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     86     }
     87 }
     88