11package com .dji .wsbridge ;
22
33import android .app .Activity ;
4+ import android .app .ActivityManager ;
5+ import android .app .AlertDialog ;
6+ import android .content .BroadcastReceiver ;
7+ import android .content .Context ;
8+ import android .content .DialogInterface ;
49import android .content .Intent ;
10+ import android .content .IntentFilter ;
11+ import android .content .SharedPreferences ;
512import android .content .pm .PackageInfo ;
613import android .content .pm .PackageManager ;
714import android .graphics .PorterDuff ;
815import android .hardware .usb .UsbManager ;
16+ import android .net .Uri ;
17+ import android .os .Build ;
918import android .os .Bundle ;
19+ import android .preference .PreferenceManager ;
1020import android .util .Log ;
21+ import android .view .View ;
1122import android .view .Window ;
23+ import android .view .animation .AlphaAnimation ;
24+ import android .view .animation .Animation ;
25+ import android .view .animation .LinearInterpolator ;
26+ import android .widget .ImageButton ;
1227import android .widget .ImageView ;
1328import android .widget .TextView ;
1429import android .widget .Toast ;
1530
1631import com .dji .wsbridge .lib .BridgeApplication ;
32+ import com .dji .wsbridge .lib .BridgeUpdateService ;
1733import com .dji .wsbridge .lib .DJILogger ;
1834import com .dji .wsbridge .lib .StreamRunner ;
1935import com .dji .wsbridge .lib .Utils ;
3248import io .reactivex .android .schedulers .AndroidSchedulers ;
3349import io .reactivex .functions .Consumer ;
3450
51+
3552public class BridgeActivity extends Activity {
3653
3754 public static final String TAG = "AndroidBridge" ;
3855 private static final int WEB_SOCKET_PORT = 9007 ;
3956 private static final Observable HEART_BEAT = Observable .timer (2 , TimeUnit .SECONDS ).repeat ().observeOn (AndroidSchedulers .mainThread ());
4057 public static AtomicBoolean isStarted = new AtomicBoolean (false );
58+ private Intent bridgeServiceIntent ;
59+ BroadcastReceiver updateAvailableReceiver ;
60+ IntentFilter updateAvailableFilter ;
61+ SharedPreferences sharedPreferences ;
62+ Context ctx ;
4163 private TextView mIPTextView ;
4264 private ImageView mRCIconView ;
4365 private ImageView mWifiIconView ;
@@ -52,15 +74,44 @@ public class BridgeActivity extends Activity {
5274 private OutputStream wsOutputStream ;
5375 private StreamRunner deviceToWSRunner ;
5476 private StreamRunner wsToDeviceRunner ;
77+ private ImageButton btnSettings , btnInstallUpdate ;
78+ private BridgeUpdateService bridgeUpdateService ;
5579
5680 //region -------------------------------------- Activity Callbacks and Helpers ---------------------------------------------
5781 @ Override
5882 protected void onCreate (Bundle savedInstanceState ) {
5983 super .onCreate (savedInstanceState );
6084 DJILogger .init ();
6185 setupViews ();
86+ sharedPreferences = PreferenceManager .getDefaultSharedPreferences (BridgeActivity .this );
6287 setupWSConnectionManager ();
6388 startHeartBeat ();
89+ setupUpdateService ();
90+
91+
92+ }
93+
94+ private void setupUpdateService () {
95+ updateAvailableReceiver = new BroadcastReceiver () {
96+ @ Override
97+ public void onReceive (Context context , Intent intent ) {
98+ btnInstallUpdate .setVisibility (View .VISIBLE );
99+ Animation mAnimation = new AlphaAnimation (1 , 0 );
100+ mAnimation .setDuration (200 );
101+ mAnimation .setInterpolator (new LinearInterpolator ());
102+ mAnimation .setRepeatCount (Animation .INFINITE );
103+ mAnimation .setRepeatMode (Animation .REVERSE );
104+
105+ btnInstallUpdate .startAnimation (mAnimation );
106+ }
107+ };
108+ updateAvailableFilter = new IntentFilter (getResources ().getString (R .string .intent_filter_update_available ));
109+
110+ bridgeUpdateService = new BridgeUpdateService (this );
111+ bridgeServiceIntent = new Intent (this , bridgeUpdateService .getClass ());
112+ if (!isMyServiceRunning (bridgeUpdateService .getClass ())) {
113+ startService (bridgeServiceIntent );
114+ }
64115
65116 }
66117
@@ -86,6 +137,7 @@ protected void onResume() {
86137 BridgeApplication .getInstance ().getBus ().register (this );
87138 DJILogger .init ();
88139 USBConnectionManager .getInstance ().init ();
140+ BridgeActivity .this .registerReceiver (updateAvailableReceiver , updateAvailableFilter );
89141 DJILogger .v (TAG , "Resumed" );
90142 }
91143
@@ -95,12 +147,15 @@ protected void onPause() {
95147 USBConnectionManager .getInstance ().destroy ();
96148 stopStreamTransfer ();
97149 BridgeApplication .getInstance ().getBus ().unregister (this );
150+
98151 super .onPause ();
152+
99153 }
100154
101155 @ Override
102156 protected void onDestroy () {
103157 DJILogger .v (TAG , "Destroyed" );
158+ this .unregisterReceiver (updateAvailableReceiver );
104159 super .onDestroy ();
105160 }
106161
@@ -110,12 +165,12 @@ protected void onDestroy() {
110165 */
111166 @ Override
112167 protected void onNewIntent (Intent intent ) {
113-
114- switch (intent .getAction ()) {
115- case UsbManager .ACTION_USB_ACCESSORY_ATTACHED :
116- BridgeApplication .getInstance ().getBus ().post (new USBConnectionManager .USBConnectionEvent (true ));
117- break ;
118- }
168+ if ( intent . getAction () != null )
169+ switch (intent .getAction ()) {
170+ case UsbManager .ACTION_USB_ACCESSORY_ATTACHED :
171+ BridgeApplication .getInstance ().getBus ().post (new USBConnectionManager .USBConnectionEvent (true ));
172+ break ;
173+ }
119174 super .onNewIntent (intent );
120175 }
121176
@@ -127,6 +182,52 @@ private void setupViews() {
127182 mRCIconView = (ImageView ) findViewById (R .id .imageViewRC );
128183 mWifiIconView = (ImageView ) findViewById (R .id .imageViewWifi );
129184 TextView mVersionTextView = (TextView ) findViewById (R .id .versionText );
185+ btnSettings = (ImageButton ) findViewById (R .id .btnSettings );
186+ btnInstallUpdate = (ImageButton ) findViewById (R .id .btnInstallUpdate );
187+ btnInstallUpdate .setVisibility (View .GONE );
188+ btnSettings .setOnClickListener (new View .OnClickListener () {
189+ @ Override
190+ public void onClick (View v ) {
191+ Intent settingsIntent = new Intent (BridgeActivity .this , SettingsActivity .class );
192+ startActivity (settingsIntent );
193+
194+ }
195+ });
196+ btnInstallUpdate .setOnClickListener (new View .OnClickListener () {
197+ @ Override
198+ public void onClick (View v ) {
199+ AlertDialog .Builder builder ;
200+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
201+ builder = new AlertDialog .Builder (BridgeActivity .this , android .R .style .Theme_Material_Dialog_Alert );
202+ } else {
203+ builder = new AlertDialog .Builder (BridgeActivity .this );
204+ }
205+ builder .setTitle ("Update avaialble !" )
206+ .setMessage ("Update is available. Do you want to update the app?" )
207+ .setPositiveButton (android .R .string .yes , new DialogInterface .OnClickListener () {
208+ public void onClick (DialogInterface dialog , int which ) {
209+ btnInstallUpdate .clearAnimation ();
210+ btnInstallUpdate .setVisibility (View .GONE );
211+ Intent install = new Intent (Intent .ACTION_VIEW );
212+ install .setFlags (Intent .FLAG_ACTIVITY_CLEAR_TASK );
213+ install .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
214+ Uri uri = Uri .parse (sharedPreferences .getString (getResources ().getString (R .string .preference_update_uri ), "" ));
215+ install .setDataAndType (uri ,
216+ sharedPreferences .getString (getResources ().getString (R .string .preference_update_mimetype ), "" ));
217+ startActivity (install );
218+ }
219+ })
220+ .setNegativeButton (android .R .string .no , new DialogInterface .OnClickListener () {
221+ public void onClick (DialogInterface dialog , int which ) {
222+ // do nothing
223+ }
224+ })
225+ .setIcon (android .R .drawable .ic_dialog_alert )
226+ .show ();
227+
228+ }
229+ });
230+
130231 // Show version number
131232 try {
132233 PackageInfo pInfo = getPackageManager ().getPackageInfo (getPackageName (), 0 );
@@ -362,4 +463,18 @@ public void run() {
362463 });
363464 }
364465 //endregion
466+
467+ private boolean isMyServiceRunning (Class <?> serviceClass ) {
468+ ActivityManager manager = (ActivityManager ) getSystemService (Context .ACTIVITY_SERVICE );
469+ for (ActivityManager .RunningServiceInfo service : manager .getRunningServices (Integer .MAX_VALUE )) {
470+ if (serviceClass .getName ().equals (service .service .getClassName ())) {
471+ Log .i ("isMyServiceRunning?" , true + "" );
472+ return true ;
473+ }
474+ }
475+ Log .i ("isMyServiceRunning?" , false + "" );
476+ return false ;
477+ }
478+
479+
365480}
0 commit comments