OSDN Git Service

Make background media saving a service
[android-x86/packages-apps-Gallery2.git] / src / com / android / camera / MediaSaveService.java
1 /*
2  * Copyright (C) 2013 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.camera;
18
19 import android.app.Service;
20 import android.content.ContentResolver;
21 import android.content.Intent;
22 import android.location.Location;
23 import android.net.Uri;
24 import android.os.AsyncTask;
25 import android.os.Binder;
26 import android.os.IBinder;
27 import android.util.Log;
28
29 import java.util.ArrayList;
30
31 public class MediaSaveService extends Service {
32     private static final int SAVE_TASK_LIMIT = 3;
33     private static final String TAG = MediaSaveService.class.getSimpleName();
34
35     private final IBinder mBinder = new LocalBinder();
36     private int mTaskNumber;
37     private Listener mListener;
38
39     interface Listener {
40         public void onQueueAvailable();
41         public void onQueueFull();
42     }
43
44     interface OnMediaSavedListener {
45         public void onMediaSaved(Uri uri);
46     }
47
48     class LocalBinder extends Binder {
49         public MediaSaveService getService() {
50             return MediaSaveService.this;
51         }
52     }
53
54     @Override
55     public IBinder onBind(Intent intent) {
56         return mBinder;
57     }
58
59     @Override
60     public int onStartCommand(Intent intent, int flag, int startId) {
61         return START_STICKY;
62     }
63
64     @Override
65     public void onDestroy() {
66     }
67
68     @Override
69     public void onCreate() {
70         mTaskNumber = 0;
71     }
72
73     public boolean isQueueFull() {
74         return (mTaskNumber >= SAVE_TASK_LIMIT);
75     }
76
77     // Runs in main thread
78     public void addImage(final byte[] data, String title, long date, Location loc,
79             int width, int height, int orientation,
80             OnMediaSavedListener l, ContentResolver resolver) {
81         if (isQueueFull()) {
82             Log.e(TAG, "Cannot add image when the queue is full");
83             return;
84         }
85         SaveTask t = new SaveTask(data, title, date, (loc == null) ? null : new Location(loc),
86                 width, height, orientation, resolver, l);
87
88         mTaskNumber++;
89         if (isQueueFull()) {
90             onQueueFull();
91         }
92         t.execute();
93     }
94
95     public void setListener(Listener l) {
96         mListener = l;
97         if (l == null) return;
98         if (isQueueFull()) {
99             l.onQueueFull();
100         } else {
101             l.onQueueAvailable();
102         }
103     }
104
105     private void onQueueFull() {
106         if (mListener != null) mListener.onQueueFull();
107     }
108
109     private void onQueueAvailable() {
110         if (mListener != null) mListener.onQueueAvailable();
111     }
112
113     private class SaveTask extends AsyncTask <Void, Void, Uri> {
114         private byte[] data;
115         private String title;
116         private long date;
117         private Location loc;
118         private int width, height;
119         private int orientation;
120         private ContentResolver resolver;
121         private OnMediaSavedListener listener;
122
123         public SaveTask(byte[] data, String title, long date, Location loc,
124                 int width, int height, int orientation, ContentResolver resolver,
125                 OnMediaSavedListener listener) {
126             this.data = data;
127             this.title = title;
128             this.date = date;
129             this.loc = loc;
130             this.width = width;
131             this.height = height;
132             this.orientation = orientation;
133             this.resolver = resolver;
134             this.listener = listener;
135         }
136
137         @Override
138         protected void onPreExecute() {
139             // do nothing.
140         }
141
142         @Override
143         protected Uri doInBackground(Void... v) {
144             return Storage.addImage(
145                     resolver, title, date, loc, orientation, data, width, height);
146         }
147
148         @Override
149         protected void onPostExecute(Uri uri) {
150             listener.onMediaSaved(uri);
151             mTaskNumber--;
152             if (mTaskNumber == SAVE_TASK_LIMIT - 1) onQueueAvailable();
153         }
154     }
155 }