Home | History | Annotate | Download | only in devicepolicy
      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 
     17 package com.android.server.devicepolicy;
     18 
     19 import android.annotation.IntDef;
     20 import android.app.Notification;
     21 import android.app.PendingIntent;
     22 import android.app.admin.DevicePolicyManager;
     23 import android.content.ComponentName;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.os.UserHandle;
     27 import android.provider.Settings;
     28 import android.text.format.DateUtils;
     29 
     30 import com.android.internal.R;
     31 
     32 import java.lang.annotation.Retention;
     33 import java.lang.annotation.RetentionPolicy;
     34 
     35 /**
     36  * Utilities class for the remote bugreport operation.
     37  */
     38 class RemoteBugreportUtils {
     39 
     40     static final int NOTIFICATION_ID = 678432343;
     41 
     42     @Retention(RetentionPolicy.SOURCE)
     43     @IntDef({
     44         DevicePolicyManager.NOTIFICATION_BUGREPORT_STARTED,
     45         DevicePolicyManager.NOTIFICATION_BUGREPORT_ACCEPTED_NOT_FINISHED,
     46         DevicePolicyManager.NOTIFICATION_BUGREPORT_FINISHED_NOT_ACCEPTED
     47     })
     48     @interface RemoteBugreportNotificationType {}
     49 
     50     static final long REMOTE_BUGREPORT_TIMEOUT_MILLIS = 10 * DateUtils.MINUTE_IN_MILLIS;
     51 
     52     static final String CTL_STOP = "ctl.stop";
     53     static final String REMOTE_BUGREPORT_SERVICE = "bugreportremote";
     54 
     55     static final String BUGREPORT_MIMETYPE = "application/vnd.android.bugreport";
     56 
     57     static Notification buildNotification(Context context,
     58             @RemoteBugreportNotificationType int type) {
     59         Intent dialogIntent = new Intent(Settings.ACTION_SHOW_REMOTE_BUGREPORT_DIALOG);
     60         dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
     61         dialogIntent.putExtra(DevicePolicyManager.EXTRA_BUGREPORT_NOTIFICATION_TYPE, type);
     62         PendingIntent pendingDialogIntent = PendingIntent.getActivityAsUser(context, type,
     63                 dialogIntent, 0, null, UserHandle.CURRENT);
     64 
     65         Notification.Builder builder = new Notification.Builder(context)
     66                 .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
     67                 .setOngoing(true)
     68                 .setLocalOnly(true)
     69                 .setPriority(Notification.PRIORITY_HIGH)
     70                 .setContentIntent(pendingDialogIntent)
     71                 .setColor(context.getColor(
     72                         com.android.internal.R.color.system_notification_accent_color));
     73 
     74         if (type == DevicePolicyManager.NOTIFICATION_BUGREPORT_ACCEPTED_NOT_FINISHED) {
     75             builder.setContentTitle(context.getString(
     76                         R.string.sharing_remote_bugreport_notification_title))
     77                     .setProgress(0, 0, true);
     78         } else if (type == DevicePolicyManager.NOTIFICATION_BUGREPORT_STARTED) {
     79             builder.setContentTitle(context.getString(
     80                         R.string.taking_remote_bugreport_notification_title))
     81                     .setProgress(0, 0, true);
     82         } else if (type == DevicePolicyManager.NOTIFICATION_BUGREPORT_FINISHED_NOT_ACCEPTED) {
     83             PendingIntent pendingIntentAccept = PendingIntent.getBroadcast(context, NOTIFICATION_ID,
     84                     new Intent(DevicePolicyManager.ACTION_BUGREPORT_SHARING_ACCEPTED),
     85                     PendingIntent.FLAG_CANCEL_CURRENT);
     86             PendingIntent pendingIntentDecline = PendingIntent.getBroadcast(context,
     87                     NOTIFICATION_ID, new Intent(
     88                             DevicePolicyManager.ACTION_BUGREPORT_SHARING_DECLINED),
     89                     PendingIntent.FLAG_CANCEL_CURRENT);
     90             builder.addAction(new Notification.Action.Builder(null /* icon */, context.getString(
     91                         R.string.decline_remote_bugreport_action), pendingIntentDecline).build())
     92                     .addAction(new Notification.Action.Builder(null /* icon */, context.getString(
     93                         R.string.share_remote_bugreport_action), pendingIntentAccept).build())
     94                     .setContentTitle(context.getString(
     95                         R.string.share_remote_bugreport_notification_title))
     96                     .setContentText(context.getString(
     97                         R.string.share_remote_bugreport_notification_message_finished))
     98                     .setStyle(new Notification.BigTextStyle().bigText(context.getString(
     99                         R.string.share_remote_bugreport_notification_message_finished)));
    100         }
    101 
    102         return builder.build();
    103     }
    104 }
    105 
    106