xie
2024-11-23 3fdfdea0721fe7a36f6aaa509075f01a194f6748
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.android.camera.processing;
 
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Process;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
import com.android.camera.app.CameraServices;
import com.android.camera.app.CameraServicesImpl;
import com.android.camera.debug.Log;
import com.android.camera.session.CaptureSession;
import com.android.camera.session.CaptureSession.ProgressListener;
import com.android.camera.session.CaptureSessionManager;
import com.android.camera.util.AndroidServices;
import com.android.camera2.R;
 
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * A service that processes a {@code ProcessingTask}. The service uses a fifo
 * queue so that only one {@code ProcessingTask} is processed at a time.
 * <p>
 * The service is meant to be called via {@code ProcessingService.addTask},
 * which takes care of starting the service and enqueueing the
 * {@code ProcessingTask} task:
 *
 * <pre>
 * {@code
 * ProcessingTask task = new MyProcessingTask(...);
 * ProcessingService.addTask(task);
 * }
 * </pre>
 */
public class ProcessingService extends Service implements ProgressListener {
    /**
     * Class used to receive broadcast and control the service accordingly.
     */
    public class ServiceController extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == ACTION_PAUSE_PROCESSING_SERVICE) {
                ProcessingService.this.pause();
            } else if (intent.getAction() == ACTION_RESUME_PROCESSING_SERVICE) {
                ProcessingService.this.resume();
            }
        }
    }
 
    private static final Log.Tag TAG = new Log.Tag("ProcessingService");
    private static final int THREAD_PRIORITY = Process.THREAD_PRIORITY_BACKGROUND;
    private static final int CAMERA_NOTIFICATION_ID = 2;
    private Notification.Builder mNotificationBuilder;
    private NotificationManager mNotificationManager;
 
    /** Sending this broadcast intent will cause the processing to pause. */
    public static final String ACTION_PAUSE_PROCESSING_SERVICE =
            "com.android.camera.processing.PAUSE";
    /**
     * Sending this broadcast intent will cause the processing to resume after
     * it has been paused.
     */
    public static final String ACTION_RESUME_PROCESSING_SERVICE =
            "com.android.camera.processing.RESUME";
 
    private WakeLock mWakeLock;
    private final ServiceController mServiceController = new ServiceController();
 
    /** Manages the capture session. */
    private CaptureSessionManager mSessionManager;
 
    private ProcessingServiceManager mProcessingServiceManager;
    private Thread mProcessingThread;
    private volatile boolean mPaused = false;
    private ProcessingTask mCurrentTask;
    private final Lock mSuspendStatusLock = new ReentrantLock();
 
    @Override
    public void onCreate() {
        mProcessingServiceManager = ProcessingServiceManager.instance();
        mSessionManager = getServices().getCaptureSessionManager();
 
        // Keep CPU awake while allowing screen and keyboard to switch off.
        PowerManager powerManager = AndroidServices.instance().providePowerManager();
        mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG.toString());
        mWakeLock.acquire();
 
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_PAUSE_PROCESSING_SERVICE);
        intentFilter.addAction(ACTION_RESUME_PROCESSING_SERVICE);
        LocalBroadcastManager.getInstance(this).registerReceiver(mServiceController, intentFilter);
        mNotificationBuilder = createInProgressNotificationBuilder();
        mNotificationManager = AndroidServices.instance().provideNotificationManager();
    }
 
    @Override
    public void onDestroy() {
        Log.d(TAG, "Shutting down");
        // TODO: Cancel session in progress...
 
        // Unlock the power manager, i.e. let power management kick in if
        // needed.
        if (mWakeLock.isHeld()) {
            mWakeLock.release();
        }
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mServiceController);
        stopForeground(true);
    }
 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Starting in foreground.");
 
        // We need to start this service in foreground so that it's not getting
        // killed easily when memory pressure is building up.
        startForeground(CAMERA_NOTIFICATION_ID, mNotificationBuilder.build());
 
        asyncProcessAllTasksAndShutdown();
 
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }
 
    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null.
        return null;
    }
 
    private void pause() {
        Log.d(TAG, "Pausing");
        try {
            mSuspendStatusLock.lock();
            mPaused = true;
            if (mCurrentTask != null) {
                mCurrentTask.suspend();
            }
        } finally {
            mSuspendStatusLock.unlock();
        }
    }
 
    private void resume() {
        Log.d(TAG, "Resuming");
        try {
            mSuspendStatusLock.lock();
            mPaused = false;
            if (mCurrentTask != null) {
                mCurrentTask.resume();
            }
        } finally {
            mSuspendStatusLock.unlock();
        }
    }
 
    /**
     * Starts a thread to process all tasks. When no more tasks are in the
     * queue, it exits the thread and shuts down the service.
     */
    private void asyncProcessAllTasksAndShutdown() {
        if (mProcessingThread != null) {
            return;
        }
        mProcessingThread = new Thread("CameraProcessingThread") {
            @Override
            public void run() {
                // Set the thread priority
                android.os.Process.setThreadPriority(THREAD_PRIORITY);
 
                ProcessingTask task;
                while ((task = mProcessingServiceManager.popNextSession()) != null) {
                    mCurrentTask = task;
                    try {
                        mSuspendStatusLock.lock();
                        if (mPaused) {
                            mCurrentTask.suspend();
                        }
                    } finally {
                        mSuspendStatusLock.unlock();
                    }
                    processAndNotify(task);
                }
                stopSelf();
            }
        };
        mProcessingThread.start();
    }
 
    /**
     * Processes a {@code ProcessingTask} and updates the notification bar.
     */
    void processAndNotify(ProcessingTask task) {
        if (task == null) {
            Log.e(TAG, "Reference to ProcessingTask is null");
            return;
        }
        CaptureSession session = task.getSession();
 
        // TODO: Get rid of this null check. There should not be a task without
        // a session.
        if (session == null) {
            // TODO: Timestamp is not required right now, refactor this to make it clearer.
            session = mSessionManager.createNewSession(task.getName(), 0, task.getLocation());
        }
        resetNotification();
 
        // Adding the listener also causes it to get called for the session's
        // current status message and percent completed.
        session.addProgressListener(this);
 
        System.gc();
        Log.d(TAG, "Processing start");
        task.process(this, getServices(), session);
        Log.d(TAG, "Processing done");
    }
 
    private void resetNotification() {
        mNotificationBuilder.setContentText("…").setProgress(100, 0, false);
        postNotification();
    }
 
    /**
     * Returns the common camera services.
     */
    private CameraServices getServices() {
        return CameraServicesImpl.instance();
    }
 
    private void postNotification() {
        mNotificationManager.notify(CAMERA_NOTIFICATION_ID, mNotificationBuilder.build());
    }
 
    /**
     * Creates a notification to indicate that a computation is in progress.
     */
    private Notification.Builder createInProgressNotificationBuilder() {
        return new Notification.Builder(this)
                .setSmallIcon(R.drawable.ic_notification)
                .setWhen(System.currentTimeMillis())
                .setOngoing(true)
                .setContentTitle(this.getText(R.string.app_name));
    }
 
    @Override
    public void onProgressChanged(int progress) {
        mNotificationBuilder.setProgress(100, progress, false);
        postNotification();
    }
 
    @Override
    public void onStatusMessageChanged(int messageId) {
        mNotificationBuilder.setContentText(messageId > 0 ? getString(messageId) : "");
        postNotification();
    }
}