77package org .deviceconnect .android .deviceplugin .theta ;
88
99import android .annotation .TargetApi ;
10- import android .content .BroadcastReceiver ;
11- import android .content .Context ;
1210import android .content .Intent ;
13- import android .content .IntentFilter ;
14- import android .net .ConnectivityManager ;
15- import android .net .Network ;
16- import android .net .NetworkCapabilities ;
17- import android .net .NetworkInfo ;
18- import android .net .NetworkRequest ;
1911import android .net .wifi .ScanResult ;
20- import android .net .wifi .WifiInfo ;
21- import android .net .wifi .WifiManager ;
22- import android .net .wifi .WifiNetworkSpecifier ;
2312import android .os .Build ;
24- import android .os .Handler ;
25- import android .os .Looper ;
26- import android .support .annotation .NonNull ;
27- import android .util .Log ;
28- import android .widget .Toast ;
2913
3014import com .theta360 .lib .PtpipInitiator ;
3115import com .theta360 .lib .ThetaException ;
3216
3317import org .deviceconnect .android .deviceplugin .theta .core .ThetaDevice ;
3418import org .deviceconnect .android .deviceplugin .theta .core .ThetaDeviceClient ;
3519import org .deviceconnect .android .deviceplugin .theta .core .ThetaDeviceEventListener ;
36- import org .deviceconnect .android .deviceplugin .theta .core .ThetaDeviceException ;
37- import org .deviceconnect .android .deviceplugin .theta .core .ThetaDeviceFactory ;
3820import org .deviceconnect .android .deviceplugin .theta .core .ThetaDeviceManager ;
39- import org .deviceconnect .android .deviceplugin .theta .core .ThetaV ;
40- import org .deviceconnect .android .deviceplugin .theta .core .wifi .WifiStateEventListener ;
4121import org .deviceconnect .android .deviceplugin .theta .profile .ThetaMediaStreamRecordingProfile ;
4222import org .deviceconnect .android .deviceplugin .theta .profile .ThetaOmnidirectionalImageProfile ;
4323import org .deviceconnect .android .deviceplugin .theta .profile .ThetaSystemProfile ;
5131import org .deviceconnect .android .provider .FileManager ;
5232import org .deviceconnect .android .service .DConnectService ;
5333
54- import java .io .IOException ;
55- import java .net .Socket ;
5634import java .util .ArrayList ;
5735import java .util .List ;
5836import java .util .logging .Logger ;
5937
60- import javax .net .SocketFactory ;
61-
6238/**
6339 * Theta Device Service.
6440 *
6541 * @author NTT DOCOMO, INC.
6642 */
67- public class ThetaDeviceService extends DConnectMessageService
68- implements ThetaDeviceEventListener {
43+ public class ThetaDeviceService extends DConnectMessageService implements ThetaDeviceEventListener {
6944
7045 public static final String ACTION_CONNECT_WIFI = "action.CONNECT_WIFI" ;
7146
@@ -78,29 +53,6 @@ public class ThetaDeviceService extends DConnectMessageService
7853 private ThetaDeviceClient mClient ;
7954 private FileManager mFileMgr ;
8055 private ThetaMediaStreamRecordingProfile mThetaMediaStreamRecording ;
81- private WifiStateEventListener mListener ;
82- private BroadcastReceiver mWifiReceiver = new BroadcastReceiver () {
83- @ Override
84- public void onReceive (Context context , Intent intent ) {
85- String action = intent .getAction ();
86- if (WifiManager .NETWORK_STATE_CHANGED_ACTION .equals (action )) {
87- WifiInfo wifiInfo = intent .getParcelableExtra (WifiManager .EXTRA_WIFI_INFO );
88- mListener .onNetworkChanged (wifiInfo );
89- } else if (WifiManager .WIFI_STATE_CHANGED_ACTION .equals (action )) {
90- int state = intent .getIntExtra (WifiManager .EXTRA_WIFI_STATE , WifiManager .WIFI_STATE_UNKNOWN );
91- switch (state ) {
92- case WifiManager .WIFI_STATE_DISABLED :
93- mListener .onWiFiDisabled ();
94- break ;
95- case WifiManager .WIFI_STATE_ENABLED :
96- mListener .onWiFiEnabled ();
97- break ;
98- default :
99- break ;
100- }
101- }
102- }
103- };
10456
10557 @ Override
10658 public int onStartCommand (final Intent intent , final int flags , final int startId ) {
@@ -115,28 +67,21 @@ public int onStartCommand(final Intent intent, final int flags, final int startI
11567 @ Override
11668 public void onCreate () {
11769 super .onCreate ();
118- mListener = (( ThetaDeviceApplication ) getApplication ()). getDeviceManager ();
70+
11971 ThetaDeviceApplication app = (ThetaDeviceApplication ) getApplication ();
12072 mDeviceMgr = app .getDeviceManager ();
12173 mDeviceMgr .registerDeviceEventListener (this );
122- mDeviceMgr .checkConnectedDevice ();
12374 mDeviceMgr .startDeviceDetection ();
12475 mClient = new ThetaDeviceClient (mDeviceMgr );
12576 mFileMgr = new FileManager (this );
12677
12778 EventManager .INSTANCE .setController (new MemoryCacheController ());
12879
12980 getServiceProvider ().addService (new ThetaImageService (app .getHeadTracker ()));
130-
131- IntentFilter filter = new IntentFilter ();
132- filter .addAction (WifiManager .NETWORK_STATE_CHANGED_ACTION );
133- filter .addAction (WifiManager .WIFI_STATE_CHANGED_ACTION );
134- registerReceiver (mWifiReceiver , filter );
13581 }
13682
13783 @ Override
13884 public void onDestroy () {
139- unregisterReceiver (mWifiReceiver );
14085 mDeviceMgr .dispose ();
14186 mDeviceMgr .unregisterDeviceEventListener (this );
14287 try {
@@ -236,41 +181,7 @@ private void resetPluginResource() {
236181
237182 @ TargetApi (Build .VERSION_CODES .Q )
238183 private void connectWifi (final ScanResult result ) {
239- WifiNetworkSpecifier specifier = new WifiNetworkSpecifier .Builder ()
240- .setSsid (result .SSID )
241- .setWpa2Passphrase (parsePassword (result .SSID ))
242- .build ();
243- NetworkRequest request = new NetworkRequest .Builder ()
244- .addTransportType (NetworkCapabilities .TRANSPORT_WIFI )
245- .removeCapability (NetworkCapabilities .NET_CAPABILITY_INTERNET )
246- .setNetworkSpecifier (specifier )
247- .build ();
248- requestNetwork (request );
249- }
250-
251- @ TargetApi (Build .VERSION_CODES .Q )
252- private boolean requestNetwork (final NetworkRequest request ) {
253- ConnectivityManager connectivityManager = getConnectivityManager ();
254- if (connectivityManager == null ) {
255- return false ;
256- }
257- final ConnectivityManager .NetworkCallback callback = new ConnectivityManager .NetworkCallback () {
258- @ Override
259- public void onAvailable (final @ NonNull Network network ) {
260- super .onAvailable (network );
261-
262- Toast .makeText (getApplicationContext (), "onAvailable" , Toast .LENGTH_LONG ).show ();
263- }
264- };
265- connectivityManager .requestNetwork (request , callback , new Handler (Looper .getMainLooper ()));
266- return true ;
267- }
268-
269- private String parsePassword (final String ssid ) {
270- return ssid .substring (7 , 7 + 8 );
271- }
272-
273- private ConnectivityManager getConnectivityManager () {
274- return (ConnectivityManager ) getSystemService (Context .CONNECTIVITY_SERVICE );
184+ ThetaDeviceManager deviceManager = ((ThetaDeviceApplication ) getApplication ()).getDeviceManager ();
185+ deviceManager .requestNetwork (result .SSID );
275186 }
276187}
0 commit comments