1/*
2 /*
3 * Copyright (C) 2011 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.emailcommon.service;
19
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.ServiceConnection;
24import android.content.pm.ProviderInfo;
25import android.os.AsyncTask;
26import android.os.Debug;
27import android.os.IBinder;
28import android.os.Looper;
29import android.os.RemoteException;
30
31import com.android.emailcommon.provider.EmailContent;
32import com.android.mail.utils.LogUtils;
33
34/**
35 * ServiceProxy is a superclass for proxy objects which make a single call to a service. It handles
36 * connecting to the service, running a task supplied by the subclass when the connection is ready,
37 * and disconnecting from the service afterwards. ServiceProxy objects cannot be reused (trying to
38 * do so generates an {@link IllegalStateException}).
39 *
40 * Subclasses must override {@link #onConnected} to store the binder. Then, when the subclass wants
41 * to make a service call, it should call {@link #setTask}, supplying the {@link ProxyTask} that
42 * should run when the connection is ready. {@link ProxyTask#run} should implement the necessary
43 * logic to make the call on the service.
44 */
45
46public abstract class ServiceProxy {
47    public static final String EXTRA_FORCE_SHUTDOWN = "ServiceProxy.FORCE_SHUTDOWN";
48
49    private static final boolean DEBUG_PROXY = false; // DO NOT CHECK THIS IN SET TO TRUE
50    private final String mTag;
51
52    private final Context mContext;
53    protected final Intent mIntent;
54    private ProxyTask mTask;
55    private String mName = " unnamed";
56    private final ServiceConnection mConnection = new ProxyConnection();
57    // Service call timeout (in seconds)
58    private int mTimeout = 45;
59    private long mStartTime;
60    private boolean mTaskSet = false;
61    private boolean mTaskCompleted = false;
62
63    public static Intent getIntentForEmailPackage(Context context, String actionName) {
64        /**
65         * We want to scope the intent so that only the Email app will handle it. Unfortunately
66         * we found that there are many instances where the package name of the Email app is
67         * not what we expect. The easiest way to find the package of the correct app is to
68         * see who is the EmailContent.AUTHORITY as there is only one app that can implement
69         * the content provider for this authority and this is the right app to handle this intent.
70         */
71        final Intent intent = new Intent(EmailContent.EMAIL_PACKAGE_NAME + "." + actionName);
72        final ProviderInfo info = context.getPackageManager().resolveContentProvider(
73                EmailContent.AUTHORITY, 0);
74        if (info != null) {
75            final String packageName = info.packageName;
76            intent.setPackage(packageName);
77        } else {
78            LogUtils.e(LogUtils.TAG, "Could not find the Email Content Provider");
79        }
80        return intent;
81    }
82
83    /**
84     * This function is called after the proxy connects to the service but before it runs its task.
85     * Subclasses must override this to store the binder correctly.
86     * @param binder The service IBinder.
87     */
88    public abstract void onConnected(IBinder binder);
89
90    public ServiceProxy(Context _context, Intent _intent) {
91        mContext = _context;
92        mIntent = _intent;
93        mTag = getClass().getSimpleName();
94        if (Debug.isDebuggerConnected()) {
95            mTimeout <<= 2;
96        }
97    }
98
99    private class ProxyConnection implements ServiceConnection {
100        @Override
101        public void onServiceConnected(ComponentName name, IBinder binder) {
102            if (DEBUG_PROXY) {
103                LogUtils.v(mTag, "Connected: " + name.getShortClassName() + " at " +
104                        (System.currentTimeMillis() - mStartTime) + "ms");
105            }
106
107            // Let subclasses handle the binder.
108            onConnected(binder);
109
110            // Do our work in another thread.
111            new AsyncTask<Void, Void, Void>() {
112                @Override
113                protected Void doInBackground(Void... params) {
114                    try {
115                        mTask.run();
116                    } catch (RemoteException e) {
117                    }
118                    try {
119                        // Each ServiceProxy handles just one task, so we unbind after we're
120                        // done with our work.
121                        mContext.unbindService(mConnection);
122                    } catch (RuntimeException e) {
123                        // The exceptions that are thrown here look like IllegalStateException,
124                        // IllegalArgumentException and RuntimeException. Catching RuntimeException
125                        // which get them all. Reasons for these exceptions include services that
126                        // have already been stopped or unbound. This can happen if the user ended
127                        // the activity that was using the service. This is harmless, but we've got
128                        // to catch it.
129                        LogUtils.e(mTag, e, "RuntimeException when trying to unbind from service");
130                    }
131                    mTaskCompleted = true;
132                    synchronized(mConnection) {
133                        if (DEBUG_PROXY) {
134                            LogUtils.v(mTag, "Task " + mName + " completed; disconnecting");
135                        }
136                        mConnection.notify();
137                    }
138                    return null;
139                }
140            }.execute();
141        }
142
143        @Override
144        public void onServiceDisconnected(ComponentName name) {
145            if (DEBUG_PROXY) {
146                LogUtils.v(mTag, "Disconnected: " + name.getShortClassName() + " at " +
147                        (System.currentTimeMillis() - mStartTime) + "ms");
148            }
149        }
150    }
151
152    protected interface ProxyTask {
153        public void run() throws RemoteException;
154    }
155
156    public ServiceProxy setTimeout(int secs) {
157        mTimeout = secs;
158        return this;
159    }
160
161    public int getTimeout() {
162        return mTimeout;
163    }
164
165    protected boolean setTask(ProxyTask task, String name) throws IllegalStateException {
166        if (mTaskSet) {
167            throw new IllegalStateException("Cannot call setTask twice on the same ServiceProxy.");
168        }
169        mTaskSet = true;
170        mName = name;
171        mTask = task;
172        mStartTime = System.currentTimeMillis();
173        if (DEBUG_PROXY) {
174            LogUtils.v(mTag, "Bind requested for task " + mName);
175        }
176        return mContext.bindService(mIntent, mConnection, Context.BIND_AUTO_CREATE);
177    }
178
179    /**
180     * Callers that want to wait on the {@link ProxyTask} should call this immediately after calling
181     * {@link #setTask}. This will wait until the task completes, up to the timeout (which can be
182     * set with {@link #setTimeout}).
183     */
184    protected void waitForCompletion() {
185        /*
186         * onServiceConnected() is always called on the main thread, and we block the current thread
187         * for up to 10 seconds as a timeout. If we're currently on the main thread,
188         * onServiceConnected() is not called until our timeout elapses (and the UI is frozen for
189         * the duration).
190         */
191        if (Looper.myLooper() == Looper.getMainLooper()) {
192            throw new IllegalStateException("This cannot be called on the main thread.");
193        }
194
195        synchronized (mConnection) {
196            long time = System.currentTimeMillis();
197            try {
198                if (DEBUG_PROXY) {
199                    LogUtils.v(mTag, "Waiting for task " + mName + " to complete...");
200                }
201                mConnection.wait(mTimeout * 1000L);
202            } catch (InterruptedException e) {
203                // Can be ignored safely
204            }
205            if (DEBUG_PROXY) {
206                LogUtils.v(mTag, "Wait for " + mName +
207                        (mTaskCompleted ? " finished in " : " timed out in ") +
208                        (System.currentTimeMillis() - time) + "ms");
209            }
210        }
211    }
212
213    /**
214     * Connection test; return indicates whether the remote service can be connected to
215     * @return the result of trying to connect to the remote service
216     */
217    public boolean test() {
218        try {
219            return setTask(new ProxyTask() {
220                @Override
221                public void run() throws RemoteException {
222                    if (DEBUG_PROXY) {
223                        LogUtils.v(mTag, "Connection test succeeded in " +
224                                (System.currentTimeMillis() - mStartTime) + "ms");
225                    }
226                }
227            }, "test");
228        } catch (Exception e) {
229            // For any failure, return false.
230            return false;
231        }
232    }
233}
234