Java tutorial
/* IRKitDeviceService.java Copyright (c) 2014 NTT DOCOMO,INC. Released under the MIT license http://opensource.org/licenses/mit-license.php */ package org.deviceconnect.android.deviceplugin.irkit; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.WifiManager; import android.support.annotation.NonNull; import android.support.v4.content.LocalBroadcastManager; import org.deviceconnect.android.activity.PermissionUtility; import org.deviceconnect.android.deviceplugin.irkit.IRKitManager.DetectionListener; import org.deviceconnect.android.deviceplugin.irkit.data.IRKitDBHelper; import org.deviceconnect.android.deviceplugin.irkit.data.VirtualDeviceData; import org.deviceconnect.android.deviceplugin.irkit.network.WiFiUtil; import org.deviceconnect.android.deviceplugin.irkit.profile.IRKitSystemProfile; import org.deviceconnect.android.deviceplugin.irkit.service.IRKitService; import org.deviceconnect.android.deviceplugin.irkit.service.VirtualService; import org.deviceconnect.android.event.EventManager; import org.deviceconnect.android.event.cache.MemoryCacheController; import org.deviceconnect.android.localoauth.LocalOAuth2Main; import org.deviceconnect.android.message.DConnectMessageService; import org.deviceconnect.android.profile.SystemProfile; import org.deviceconnect.android.service.DConnectService; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; /** * IRKit?. * @author NTT DOCOMO, INC. */ public class IRKitDeviceService extends DConnectMessageService implements DetectionListener { /** * IRKit???????. */ public static final String ACTION_RESTART_DETECTION_IRKIT = "action.ACTION_RESTART_DETECTION_IRKIT"; /** * ???. */ public static final String ACTION_VIRTUAL_DEVICE_ADDED = "action.ACTION_VIRTUAL_DEVICE_ADDED"; /** * ???. */ public static final String ACTION_VIRTUAL_DEVICE_REMOVED = "action.ACTION_VIRTUAL_DEVICE_REMOVED"; /** * ???. */ public static final String ACTION_VIRTUAL_DEVICE_UPDATED = "action.ACTION_VIRTUAL_DEVICE_UPDATED"; /** * ??ID?????. */ public static final String EXTRA_VIRTUAL_DEVICE_ID = "extra.EXTRA_VIRTUAL_DEVICE_ID"; /** * ???. */ private final ConcurrentHashMap<String, IRKitDevice> mDevices = new ConcurrentHashMap<String, IRKitDevice>(); /** * ??SSID. */ private String mCurrentSSID; /** DB Helper. */ private IRKitDBHelper mDBHelper; /** . */ private final Logger mLogger = Logger.getLogger("irkit.dplugin"); /** ??. */ private final BroadcastReceiver mLocalBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(final Context context, final Intent intent) { if (intent == null || intent.getAction() == null) { return; } String action = intent.getAction(); if (ACTION_RESTART_DETECTION_IRKIT.equals(action)) { if (WiFiUtil.isOnWiFi(IRKitDeviceService.this)) { restartDetection(); } else { stopDetection(); } } else if (ACTION_VIRTUAL_DEVICE_ADDED.equals(action)) { String id = intent.getStringExtra(EXTRA_VIRTUAL_DEVICE_ID); if (id != null) { DConnectService service = getServiceProvider().getService(id); if (service == null) { List<VirtualDeviceData> devices = mDBHelper.getVirtualDevices(id); if (devices.size() > 0) { VirtualDeviceData device = devices.get(0); service = new VirtualService(device, mDBHelper, getServiceProvider()); getServiceProvider().addService(service); } } } } else if (ACTION_VIRTUAL_DEVICE_UPDATED.equals(action)) { String id = intent.getStringExtra(EXTRA_VIRTUAL_DEVICE_ID); if (id != null) { DConnectService service = getServiceProvider().getService(id); if (service != null) { VirtualService virtualService = (VirtualService) service; service.setOnline(virtualService.isOnline()); } } } else if (ACTION_VIRTUAL_DEVICE_REMOVED.equals(action)) { String id = intent.getStringExtra(EXTRA_VIRTUAL_DEVICE_ID); if (id != null) { DConnectService service = getServiceProvider().getService(id); if (service != null) { getServiceProvider().removeService(service); } } } } }; @Override public void onCreate() { super.onCreate(); EventManager.INSTANCE.setController(new MemoryCacheController()); mDBHelper = new IRKitDBHelper(getContext()); for (VirtualDeviceData device : mDBHelper.getVirtualDevices(null)) { VirtualService vDevice = new VirtualService(device, mDBHelper, getServiceProvider()); vDevice.setOnline(vDevice.isOnline()); getServiceProvider().addService(vDevice); } IRKitApplication app = (IRKitApplication) getApplication(); app.setIRKitDevices(mDevices); IRKitManager.INSTANCE.init(this); IRKitManager.INSTANCE.setDetectionListener(this); if (WiFiUtil.isOnWiFi(this)) { startDetection(); } IntentFilter localFilter = new IntentFilter(); localFilter.addAction(ACTION_RESTART_DETECTION_IRKIT); localFilter.addAction(ACTION_VIRTUAL_DEVICE_ADDED); localFilter.addAction(ACTION_VIRTUAL_DEVICE_REMOVED); localFilter.addAction(ACTION_VIRTUAL_DEVICE_UPDATED); LocalBroadcastManager.getInstance(this).registerReceiver(mLocalBroadcastReceiver, localFilter); WiFiUtil.checkLocationPermission(this, new PermissionUtility.PermissionRequestCallback() { @Override public void onSuccess() { mCurrentSSID = WiFiUtil.getCurrentSSID(IRKitDeviceService.this); } @Override public void onFail(@NonNull String s) { mCurrentSSID = null; } }); } @Override public int onStartCommand(final Intent intent, final int flags, final int startId) { if (intent != null) { String action = intent.getAction(); if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { if (!WiFiUtil.isOnWiFi(this) && IRKitManager.INSTANCE.isDetecting()) { stopDetection(); } else if (WiFiUtil.isOnWiFi(this) && WiFiUtil.isChangedSSID(this, mCurrentSSID)) { restartDetection(); } return START_STICKY; } } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); stopDetection(); // ???????? IRKitManager.INSTANCE.setDetectionListener(null); } @Override protected void onManagerUninstalled() { // Manager?? if (BuildConfig.DEBUG) { mLogger.info("Plug-in : onManagerUninstalled"); } } @Override protected void onManagerTerminated() { // Manager??? if (BuildConfig.DEBUG) { mLogger.info("Plug-in : onManagerTerminated"); } } @Override protected void onDevicePluginReset() { // Device Plug-in??Reset???? if (BuildConfig.DEBUG) { mLogger.info("Plug-in : onDevicePluginReset"); } } /** * ID?IRKit????. * * @param serviceId ID * @return ? */ public IRKitDevice getDevice(final String serviceId) { return mDevices.get(serviceId); } @Override protected SystemProfile getSystemProfile() { return new IRKitSystemProfile(); } @Override public void onFoundDevice(final IRKitDevice device) { updateDeviceList(device, true); DConnectService service = getServiceProvider().getService(device.getName()); if (service == null) { service = new IRKitService(device); getServiceProvider().addService(service); } service.setOnline(true); } @Override public void onLostDevice(final IRKitDevice device) { updateDeviceList(device, false); DConnectService service = getServiceProvider().getService(device.getName()); if (service != null) { service.setOnline(false); } } /** * ??. * * @param device ? * @param isOnline true??false??? */ private void updateDeviceList(final IRKitDevice device, final boolean isOnline) { synchronized (mDevices) { IRKitDevice d = mDevices.get(device.getName()); if (d != null) { if (!isOnline) { mDevices.remove(device.getName()); } } else if (isOnline) { mDevices.put(device.getName(), device); } } IRKitApplication app = (IRKitApplication) getApplication(); app.setIRKitDevices(mDevices); } /** * ?. */ private void startDetection() { mCurrentSSID = WiFiUtil.getCurrentSSID(this); IRKitManager.INSTANCE.startDetection(this); } /** * ?. */ private void stopDetection() { mCurrentSSID = null; mDevices.clear(); IRKitManager.INSTANCE.stopDetection(); } private void restartDetection() { new Thread(new Runnable() { @Override public void run() { stopDetection(); startDetection(); } }).start(); } }