blob: 9b42b782889340e718cb185725bce15266a7ec13 [file] [log] [blame]
Daichi Hirono2efe4ca2015-07-27 16:47:46 +09001/*
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
17package com.android.mtp;
18
Daichi Hironoa57d9ed2015-12-07 10:52:42 +090019import android.app.Notification;
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090020import android.app.Service;
Daichi Hironoa57d9ed2015-12-07 10:52:42 +090021import android.app.NotificationManager;
Daichi Hironofda74742016-02-01 13:00:31 +090022import android.content.ComponentName;
23import android.content.Context;
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090024import android.content.Intent;
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090025import android.os.IBinder;
26import android.util.Log;
27
28import java.io.IOException;
29
30/**
31 * Service to manage lifetime of DocumentsProvider's process.
32 * The service prevents the system from killing the process that holds USB connections. The service
33 * starts to run when the first MTP device is opened, and stops when the last MTP device is closed.
34 */
35public class MtpDocumentsService extends Service {
Daichi Hironoa57d9ed2015-12-07 10:52:42 +090036 static final String ACTION_OPEN_DEVICE = "com.android.mtp.OPEN_DEVICE";
37 static final String ACTION_CLOSE_DEVICE = "com.android.mtp.CLOSE_DEVICE";
Daichi Hironofda74742016-02-01 13:00:31 +090038 static final String ACTION_UPDATE_NOTIFICATION = "com.android.mtp.UPDATE_NOTIFICATION";
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090039 static final String EXTRA_DEVICE = "device";
40
Daichi Hironoa57d9ed2015-12-07 10:52:42 +090041 NotificationManager mNotificationManager;
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090042
43 @Override
44 public IBinder onBind(Intent intent) {
45 // The service is used via intents.
46 return null;
47 }
48
49 @Override
50 public void onCreate() {
51 super.onCreate();
Daichi Hironoa57d9ed2015-12-07 10:52:42 +090052 mNotificationManager = getSystemService(NotificationManager.class);
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090053 }
54
55 @Override
56 public int onStartCommand(Intent intent, int flags, int startId) {
Daichi Hironoe0282dd2015-11-26 15:20:08 +090057 // If intent is null, the service was restarted.
Daichi Hironofda74742016-02-01 13:00:31 +090058 if (intent == null || ACTION_UPDATE_NOTIFICATION.equals(intent.getAction())) {
59 return updateForegroundState() ? START_STICKY : START_NOT_STICKY;
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090060 }
Daichi Hironofda74742016-02-01 13:00:31 +090061 return START_NOT_STICKY;
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090062 }
63
Daichi Hironoa57d9ed2015-12-07 10:52:42 +090064 /**
65 * Updates the foreground state of the service.
66 * @return Whether the service is foreground or not.
67 */
68 private boolean updateForegroundState() {
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090069 final MtpDocumentsProvider provider = MtpDocumentsProvider.getInstance();
Daichi Hironoe6bffff2016-01-25 16:06:57 +090070 int notificationId = 0;
71 Notification notification = null;
Daichi Hironofda74742016-02-01 13:00:31 +090072 // TODO: Hide notification if the device has already been removed.
Daichi Hirono0f325372016-02-21 15:50:30 +090073 for (final MtpDeviceRecord record : provider.getOpenedDeviceRecordsCache()) {
74 final String title = getResources().getString(
75 R.string.accessing_notification_title,
76 record.name);
77 final String description = getResources().getString(
78 R.string.accessing_notification_description);
79 notificationId = record.deviceId;
80 notification = new Notification.Builder(this)
81 .setLocalOnly(true)
82 .setContentTitle(title)
83 .setContentText(description)
84 .setSmallIcon(com.android.internal.R.drawable.stat_sys_data_usb)
85 .setCategory(Notification.CATEGORY_SYSTEM)
86 .setPriority(Notification.PRIORITY_LOW)
87 .build();
88 mNotificationManager.notify(record.deviceId, notification);
Daichi Hironoa57d9ed2015-12-07 10:52:42 +090089 }
Daichi Hironoe6bffff2016-01-25 16:06:57 +090090
91 if (notification != null) {
92 startForeground(notificationId, notification);
Daichi Hironoa57d9ed2015-12-07 10:52:42 +090093 return true;
94 } else {
95 stopForeground(true /* removeNotification */);
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090096 stopSelf();
Daichi Hironoa57d9ed2015-12-07 10:52:42 +090097 return false;
Daichi Hirono2efe4ca2015-07-27 16:47:46 +090098 }
99 }
100
Daichi Hironoa57d9ed2015-12-07 10:52:42 +0900101 private static void logErrorMessage(Exception exp) {
102 if (exp.getMessage() != null) {
103 Log.e(MtpDocumentsProvider.TAG, exp.getMessage());
104 } else {
105 Log.e(MtpDocumentsProvider.TAG, exp.toString());
Daichi Hirono2efe4ca2015-07-27 16:47:46 +0900106 }
107 }
108}