2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.camera;
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;
30 * Service for saving images in the background thread.
32 public class MediaSaveService extends Service {
33 private static final int SAVE_TASK_LIMIT = 3;
34 private static final String TAG = MediaSaveService.class.getSimpleName();
36 private final IBinder mBinder = new LocalBinder();
37 private int mTaskNumber;
38 private Listener mListener;
41 public void onQueueAvailable();
42 public void onQueueFull();
45 interface OnMediaSavedListener {
46 public void onMediaSaved(Uri uri);
49 class LocalBinder extends Binder {
50 public MediaSaveService getService() {
51 return MediaSaveService.this;
56 public IBinder onBind(Intent intent) {
61 public int onStartCommand(Intent intent, int flag, int startId) {
66 public void onDestroy() {
70 public void onCreate() {
74 public boolean isQueueFull() {
75 return (mTaskNumber >= SAVE_TASK_LIMIT);
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) {
83 Log.e(TAG, "Cannot add image when the queue is full");
86 SaveTask t = new SaveTask(data, title, date, (loc == null) ? null : new Location(loc),
87 width, height, orientation, resolver, l);
96 public void setListener(Listener l) {
98 if (l == null) return;
102 l.onQueueAvailable();
106 private void onQueueFull() {
107 if (mListener != null) mListener.onQueueFull();
110 private void onQueueAvailable() {
111 if (mListener != null) mListener.onQueueAvailable();
114 private class SaveTask extends AsyncTask <Void, Void, Uri> {
116 private String title;
118 private Location loc;
119 private int width, height;
120 private int orientation;
121 private ContentResolver resolver;
122 private OnMediaSavedListener listener;
124 public SaveTask(byte[] data, String title, long date, Location loc,
125 int width, int height, int orientation, ContentResolver resolver,
126 OnMediaSavedListener listener) {
132 this.height = height;
133 this.orientation = orientation;
134 this.resolver = resolver;
135 this.listener = listener;
139 protected void onPreExecute() {
144 protected Uri doInBackground(Void... v) {
145 return Storage.addImage(
146 resolver, title, date, loc, orientation, data, width, height);
150 protected void onPostExecute(Uri uri) {
151 listener.onMediaSaved(uri);
153 if (mTaskNumber == SAVE_TASK_LIMIT - 1) onQueueAvailable();