/*
* Copyright (c) 2008-2009, Motorola, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the Motorola, Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.android.bluetooth.opp;
import com.google.android.collect.Lists;
import javax.obex.ObexTransport;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.os.Process;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Performs the background Bluetooth OPP transfer. It also starts thread to
* accept incoming OPP connection.
*/
public class BluetoothOppService extends Service {
private static final boolean D = Constants.DEBUG;
private static final boolean V = Constants.VERBOSE;
private boolean userAccepted = false;
private class BluetoothShareContentObserver extends ContentObserver {
public BluetoothShareContentObserver() {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) {
if (V) Log.v(TAG, "ContentObserver received notification");
updateFromProvider();
}
}
private static final String TAG = "BtOpp Service";
/** Observer to get notified when the content observer's data changes */
private BluetoothShareContentObserver mObserver;
/** Class to handle Notification Manager updates */
private BluetoothOppNotification mNotifier;
private boolean mPendingUpdate;
private UpdateThread mUpdateThread;
private ArrayList<BluetoothOppShareInfo> mShares;
private ArrayList<BluetoothOppBatch> mBatchs;
private BluetoothOppTransfer mTransfer;
private BluetoothOppTransfer mServerTransfer;
private int mBatchId;
/**
* Array used when extracting strings from content provider
*/
private CharArrayBuffer mOldChars;
/**
* Array used when extracting strings from content provider
*/
private CharArrayBuffer mNewChars;
private BluetoothAdapter mAdapter;
private PowerManager mPowerManager;
private BluetoothOppRfcommListener mSocketListener;
private boolean mListenStarted = false;
private boolean mMediaScanInProgress;
private int mIncomingRetries = 0;
private ObexTransport mPendingConnection = null;
/*
* TODO No support for queue incoming from multiple devices.
* Make an array list of server session to support receiving queue from
* multiple devices
*/
private BluetoothOppObexServerSession mServerSession;
@Override
public IBinder onBind(Intent arg0) {
throw new UnsupportedOperationException("Cannot bind to Bluetooth OPP Service");
}
@Override
public void onCreate() {
super.onCreate();
if (V) Log.v(TAG, "Service onCreate");
mAdapter = BluetoothAdapter.getDefaultAdapter();
mSocketListener = new BluetoothOppRfcommListener(mAdapter);
mShares = Lists.newArrayList();
mBatchs = Lists.newArrayList();
mObserver = new BluetoothShareContentObserver();
getContentResolver().registerContentObserver(BluetoothShare.CONTENT_URI, true, mObserver);
mBatchId = 1;
mNotifier = new BluetoothOppNotification(this);
mNotifier.mNotificationMgr.cancelAll();
mNotifier.updateNotification();
final ContentResolver contentResolver = getContentResolver();
new Thread("trimDatabase") {
public void run() {
trimDatabase(contentResolver);
}
}.start();
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBluetoothReceiver, filter);
synchronized (BluetoothOppService.this) {
if (mAdapter == null) {
Log.w(TAG, "Local BT device is not enabled");
} else {
startListenerDelayed();
}
}
if (V) BluetoothOppPreference.getInstance(this).dump();
updateFromProvider();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (V) Log.v(TAG, "Service onStartCommand");
int retCode = super.onStartCommand(intent, flags, startId);
if (retCode == START_STICKY) {
if (mAdapter == null) {
Log.w(TAG, "Local BT device is not enabled");
} else {
startListenerDelayed();
}
updateFromProvider();
}
return retCode;
}
private void startListenerDelayed() {
if (!mListenStarted) {
if (mAdapter.isEnabled()) {
if (V) Log.v(TAG, "Starting RfcommListener in 9 seconds");
mHandler.sendMessageDelayed(mHandler.obtainMessage(START_LISTENER), 9000);
mListenStarted = true;
}
}
}
private static final int START_LISTENER = 1;
private static final int MEDIA_SCANNED = 2;
private static final int MEDIA_SCANNED_FAILED = 3;
private static final int MSG_INCOMING_CONNECTION_RETRY = 4;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case START_LISTENER:
if (mAdapter.isEnabled()) {
startSocketListener();
}
break;
case MEDIA_SCANNED:
if (V) Log.v(TAG, "Update mInfo.id " + msg.arg1 + " for data uri= "
+ msg.obj.toString());
ContentValues updateValues = new ContentValues();
Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + msg.arg1);
updateValues.put(Constants.MEDIA_SCANNED, Constants.MEDIA_SCANNED_SCANNED_OK);
updateValues.put(BluetoothShare.URI, msg.obj.toString(