Java tutorial
/* * BluetoothHeadsetMonitorService.java * * Copyright (C) 2015,2016 Pavel Prokhorov (pavelvpster@gmail.com) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package org.interactiverobotics.headset_launcher; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.Binder; import android.os.Bundle; import android.os.CountDownTimer; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import android.util.Log; import java.util.ArrayList; /** * ???? bluetooth . * * @author pavelvpster * */ public final class BluetoothHeadsetMonitorService extends Service implements BluetoothHeadsetMonitor.BluetoothHeadsetMonitorDelegate { /** * ? ? logcat ? ??. * */ private static final String TAG = "bt_monitor_service"; /** * ? . * */ public BluetoothHeadsetMonitorService() { } /** * ?? Binder-. * */ public final class ServiceBinder extends Binder { /** * ? . * * @return String * */ public String getConnectedHeadsetName() { return mBluetoothHeadsetMonitor.getConnectedHeadsetName(); } /** * ? ? ?. * */ public void startVoiceRecognition() { mBluetoothHeadsetMonitor.startVoiceRecognition(); } /** * ? ? ?. * */ public void stopVoiceRecognition() { mBluetoothHeadsetMonitor.stopVoiceRecognition(); } /** * ? ?. * * @param callback . * */ public void addCallback(@NonNull Messenger callback) { Log.d(TAG, "Add callback: " + callback.toString()); mCallbackMessengers.add(callback); } /** * ? ?. * */ public void removeCallback(@NonNull Messenger callback) { Log.d(TAG, "Remove callback" + callback.toString()); mCallbackMessengers.remove(callback); } } /** * ?. * */ private ServiceBinder mBinder = new ServiceBinder(); /** * . * */ private final ArrayList<Messenger> mCallbackMessengers = new ArrayList<>(); @Override public IBinder onBind(Intent intent) { Log.d(TAG, "Bind"); return mBinder; } @Override public void onRebind(Intent intent) { Log.d(TAG, "Rebind"); super.onRebind(intent); } @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "Unbind"); return super.onUnbind(intent); } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "Create"); // ???? bluetooth mBluetoothHeadsetMonitor = new BluetoothHeadsetMonitor(this, this, TAG); mBluetoothHeadsetMonitor.initialize(); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "Destroy"); // ???? bluetooth mBluetoothHeadsetMonitor.dispose(); mBluetoothHeadsetMonitor = null; } /** * ???? bluetooth . * */ private BluetoothHeadsetMonitor mBluetoothHeadsetMonitor = null; /** * @see BluetoothHeadsetMonitor.BluetoothHeadsetMonitorDelegate#onBluetoothStarted() * */ @Override public void onBluetoothStarted() { if (mCallbackMessengers.isEmpty()) { // final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.headset_launcher_notification) .setContentTitle(getString(R.string.text_bluetooth_started)).setAutoCancel(true); final NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(1, builder.build()); // ? ? ? mTimer.start(); } else { // for (Messenger x : mCallbackMessengers) { try { x.send(Message.obtain(null, 1, 0, 0)); } catch (RemoteException e) { Log.e(TAG, "Callback error!"); } } } } /** * @see BluetoothHeadsetMonitor.BluetoothHeadsetMonitorDelegate#onBluetoothStopped() * */ @Override public void onBluetoothStopped() { if (mCallbackMessengers.isEmpty()) { // final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.headset_launcher_notification) .setContentTitle(getString(R.string.text_bluetooth_stopped)).setAutoCancel(true); final NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(1, builder.build()); // ? ? ? mTimer.start(); } else { // for (Messenger x : mCallbackMessengers) { try { x.send(Message.obtain(null, 2, 0, 0)); } catch (RemoteException e) { Log.e(TAG, "Callback error!"); } } } } /** * @see BluetoothHeadsetMonitor.BluetoothHeadsetMonitorDelegate#onHeadsetConnected(String) * */ @Override public void onHeadsetConnected(String name) { if (mCallbackMessengers.isEmpty()) { // final Intent notificationIntent = new Intent(this, MainActivity.class); final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.headset_launcher_notification).setContentTitle(name) .setContentText(getString(R.string.text_headset_connected)).setContentIntent(contentIntent) .setAutoCancel(true); final NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(1, builder.build()); // ? ? ? mTimer.start(); } else { // final Bundle data = new Bundle(); data.putString("ConnectedHeadsetName", name); for (Messenger x : mCallbackMessengers) { try { final Message message = Message.obtain(null, 3, 0, 0); message.setData(data); x.send(message); } catch (RemoteException e) { Log.e(TAG, "Callback error!"); } } } } /** * @see BluetoothHeadsetMonitor.BluetoothHeadsetMonitorDelegate#onHeadsetDisconnected() * */ @Override public void onHeadsetDisconnected() { if (mCallbackMessengers.isEmpty()) { // final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.headset_launcher_notification) .setContentTitle(getString(R.string.text_headset_disconnected)).setAutoCancel(true); final NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.notify(1, builder.build()); // ? ? ? mTimer.start(); } else { // for (Messenger x : mCallbackMessengers) { try { x.send(Message.obtain(null, 4, 0, 0)); } catch (RemoteException e) { Log.e(TAG, "Callback error!"); } } } } @Override public void onVoiceRecognitionStarted() { // for (Messenger x : mCallbackMessengers) { try { x.send(Message.obtain(null, 5, 0, 0)); } catch (RemoteException e) { Log.e(TAG, "Callback error!"); } } } @Override public void onVoiceRecognitionStopped() { // for (Messenger x : mCallbackMessengers) { try { x.send(Message.obtain(null, 6, 0, 0)); } catch (RemoteException e) { Log.e(TAG, "Callback error!"); } } } /** * ? ?. * * @// TODO: 01.01.16 Handler.postDelayed(Runnable) * */ private CountDownTimer mTimer = new CountDownTimer(Constants.NOTIFICATION_TIMEOUT * 1000, 1000) { @Override public void onTick(long millisUntilFinished) { Log.d(TAG, "Tick"); } @Override public void onFinish() { Log.d(TAG, "Finish"); // ? final NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE); notificationManager.cancel(1); } }; }