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;
29 import java.util.ArrayList;
31 public class MediaSaveService extends Service {
32 private static final int SAVE_TASK_LIMIT = 3;
33 private static final String TAG = MediaSaveService.class.getSimpleName();
35 private final IBinder mBinder = new LocalBinder();
36 private int mTaskNumber;
37 private Listener mListener;
40 public void onQueueAvailable();
41 public void onQueueFull();
44 interface OnMediaSavedListener {
45 public void onMediaSaved(Uri uri);
48 class LocalBinder extends Binder {
49 public MediaSaveService getService() {
50 return MediaSaveService.this;
55 public IBinder onBind(Intent intent) {
60 public int onStartCommand(Intent intent, int flag, int startId) {
65 public void onDestroy() {
69 public void onCreate() {
73 public boolean isQueueFull() {
74 return (mTaskNumber >= SAVE_TASK_LIMIT);
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) {
82 Log.e(TAG, "Cannot add image when the queue is full");
85 SaveTask t = new SaveTask(data, title, date, (loc == null) ? null : new Location(loc),
86 width, height, orientation, resolver, l);
95 public void setListener(Listener l) {
97 if (l == null) return;
101 l.onQueueAvailable();
105 private void onQueueFull() {
106 if (mListener != null) mListener.onQueueFull();
109 private void onQueueAvailable() {
110 if (mListener != null) mListener.onQueueAvailable();
113 private class SaveTask extends AsyncTask <Void, Void, Uri> {
115 private String title;
117 private Location loc;
118 private int width, height;
119 private int orientation;
120 private ContentResolver resolver;
121 private OnMediaSavedListener listener;
123 public SaveTask(byte[] data, String title, long date, Location loc,
124 int width, int height, int orientation, ContentResolver resolver,
125 OnMediaSavedListener listener) {
131 this.height = height;
132 this.orientation = orientation;
133 this.resolver = resolver;
134 this.listener = listener;
138 protected void onPreExecute() {
143 protected Uri doInBackground(Void... v) {
144 return Storage.addImage(
145 resolver, title, date, loc, orientation, data, width, height);
149 protected void onPostExecute(Uri uri) {
150 listener.onMediaSaved(uri);
152 if (mTaskNumber == SAVE_TASK_LIMIT - 1) onQueueAvailable();