1919
2020package com .hmdm .pager ;
2121
22- import android .content .BroadcastReceiver ;
2322import android .content .Context ;
2423import android .content .Intent ;
25- import android .content .IntentFilter ;
2624import android .os .AsyncTask ;
2725import android .os .Build ;
2826import android .os .Bundle ;
3735
3836import androidx .appcompat .app .AppCompatActivity ;
3937import androidx .appcompat .widget .Toolbar ;
40- import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
4138import androidx .recyclerview .widget .DividerItemDecoration ;
4239import androidx .recyclerview .widget .LinearLayoutManager ;
4340import androidx .recyclerview .widget .RecyclerView ;
4441
42+ import com .hmdm .HeadwindMDM ;
4543import com .hmdm .MDMException ;
44+ import com .hmdm .MDMPushHandler ;
45+ import com .hmdm .MDMPushMessage ;
4646import com .hmdm .MDMService ;
4747import com .hmdm .pager .db .DatabaseHelper ;
4848import com .hmdm .pager .db .MessageTable ;
5555import java .util .Date ;
5656import java .util .List ;
5757
58- public class MainActivity extends AppCompatActivity implements MDMService . ResultHandler {
58+ public class MainActivity extends AppCompatActivity implements HeadwindMDM . EventHandler {
5959
6060 private SettingsHelper settings ;
6161
62- private MDMService mdmService ;
62+ private HeadwindMDM headwindMDM ;
6363 private boolean mdmConnected = false ;
6464
6565 private TextView emptyTextView ;
6666 private RecyclerView recyclerView ;
6767 private MessageAdapter adapter ;
6868 private RecyclerView .LayoutManager layoutManager ;
69+ private Handler receiveHandler = new Handler ();
6970
70- private BroadcastReceiver newMessageReceiver = new BroadcastReceiver () {
71+ private MDMPushHandler mdmPushHandler = new MDMPushHandler () {
7172 @ Override
72- public void onReceive (Context context , Intent intent ) {
73- if (intent .getAction ().equals (Const .ACTION_NEW_MESSAGE )) {
73+ public void onMessageReceived (MDMPushMessage mdmPushMessage ) {
74+ // We need to save incoming message in the database by the service,
75+ // So we implement a delay here
76+ receiveHandler .postDelayed (() -> {
7477 adapter .updateMessages ();
7578 adapter .notifyDataSetChanged ();
7679 updateItemState ();
7780 notifyMessagesRead ();
78- }
81+ }, 300 );
7982 }
8083 };
8184
@@ -100,7 +103,7 @@ protected void onCreate(Bundle savedInstanceState) {
100103 recyclerView .addItemDecoration (dividerItemDecoration );
101104
102105 settings = SettingsHelper .getInstance (this );
103- mdmService = MDMService .getInstance ();
106+ headwindMDM = HeadwindMDM .getInstance ();
104107
105108 Intent intent = new Intent (this , PagerService .class );
106109 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
@@ -141,27 +144,31 @@ protected void onNewIntent(Intent intent) {
141144 protected void onResume () {
142145 super .onResume ();
143146
144- if (mdmConnected ) {
145- queryMdm ();
147+ String [] messageTypes = {Const .PUSH_MESSAGE_TYPE };
148+ mdmPushHandler .register (messageTypes , this );
149+
150+ if (!headwindMDM .isConnected ()) {
151+ if (!headwindMDM .connect (this , this )) {
152+ // Your application is running outside Headwind MDM
153+ Toast .makeText (MainActivity .this , getString (R .string .mdm_connect_error ), Toast .LENGTH_LONG ).show ();
154+ }
146155 } else {
147- mdmService .connect (this , this );
156+ // Already connected, but settings may have changed
157+ // when our app was in the background, so reload them
158+ queryMdm ();
148159 }
149160 updateItemState ();
150161
151162 adapter .updateMessages ();
152163 adapter .notifyDataSetChanged ();
153164
154- LocalBroadcastManager .getInstance (this ).registerReceiver (newMessageReceiver ,
155- new IntentFilter (Const .ACTION_NEW_MESSAGE ));
156-
157165 notifyMessagesRead ();
158166 }
159167
160168 @ Override
161169 protected void onPause () {
162170 super .onPause ();
163-
164- LocalBroadcastManager .getInstance (this ).unregisterReceiver (newMessageReceiver );
171+ mdmPushHandler .unregister (this );
165172 }
166173
167174 private void updateItemState () {
@@ -217,31 +224,22 @@ private void queryMdm() {
217224 }
218225
219226 @ Override
220- public void onMDMConnected () {
221- mdmConnected = true ;
222- MDMService .Log .i (Const .LOG_TAG , "activity connected to Headwind MDM" );
223-
227+ public void onHeadwindMDMConnected () {
228+ // Connected to Headwind MDM, now you can load settings and use other MDM functions
229+ MDMService .Log .i (Const .LOG_TAG , "Activity connected to Headwind MDM" );
224230 queryMdm ();
225231 }
226232
227233 @ Override
228- public void onMDMDisconnected () {
229- mdmConnected = false ;
230-
231- // Reconnect (this could be after crash of Headwind MDM!)
232- MDMService .Log .i (Const .LOG_TAG , "activity disconnected from Headwind MDM" );
233- new Handler ().postDelayed (new MDMReconnectRunnable (), Const .HMDM_RECONNECT_DELAY_FIRST );
234+ public void onHeadwindMDMDisconnected () {
235+ MDMService .Log .i (Const .LOG_TAG , "Activity disconnected from Headwind MDM" );
234236 }
235237
236- public class MDMReconnectRunnable implements Runnable {
237- @ Override
238- public void run () {
239- if (!mdmService .connect (MainActivity .this , MainActivity .this )) {
240- // Retry in 1 minute
241- MDMService .Log .i (Const .LOG_TAG , "Failed to connect to Headwind MDM, scheduling connection" );
242- new Handler ().postDelayed (this , Const .HMDM_RECONNECT_DELAY_NEXT );
243- }
244- }
238+ @ Override
239+ public void onHeadwindMDMConfigChanged () {
240+ // Settings were changed on the server, you need to reload them
241+ MDMService .Log .i (Const .LOG_TAG , "Reloading configuration from Headwind MDM" );
242+ queryMdm ();
245243 }
246244
247245 public static class MessageViewHolder extends RecyclerView .ViewHolder {
0 commit comments