OSDN Git Service

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