diff --git a/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/ApplicationAdapter.java b/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/ApplicationAdapter.java index 11badd8..e59bd39 100644 --- a/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/ApplicationAdapter.java +++ b/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/ApplicationAdapter.java @@ -69,47 +69,51 @@ public View getView(int position, View convertView, ViewGroup parent) { appName.setText(app.getAppName()); appState.setText(app.getAppState().toString()); - - Bbox bbox = BboxHolder.getInstance().getBbox(); - - if (bbox != null) { - final ApplicationsManager applicationsManager = bbox.getApplicationsManager(); - - start.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - applicationsManager.startApplication(app, new CallbackHttpStatus() { - @Override - public void onResult(int i) { - applicationsManager.getApplications(new ApplicationsManager.CallbackApplications() { - @Override - public void onResult(int status, List applicationsList) { - applications = applicationsList; - notifyDataSetChanged(); - } - }); - } - }); - } - }); - - stop.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - applicationsManager.stopApplication(app, new CallbackHttpStatus() { - @Override - public void onResult(int i) { - applicationsManager.getApplications(new ApplicationsManager.CallbackApplications() { - @Override - public void onResult(int status, List applicationsList) { - applications = applicationsList; - notifyDataSetChanged(); - } - }); - } - }); - } - }); + try { + Bbox bbox = BboxHolder.getInstance().getBbox(); + + if (bbox != null) { + final ApplicationsManager applicationsManager = bbox.getApplicationsManager(); + + start.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + applicationsManager.startApplication(app, new CallbackHttpStatus() { + @Override + public void onResult(int i) { + applicationsManager.getApplications(new ApplicationsManager.CallbackApplications() { + @Override + public void onResult(int status, List applicationsList) { + applications = applicationsList; + notifyDataSetChanged(); + } + }); + } + }); + } + }); + + stop.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + applicationsManager.stopApplication(app, new CallbackHttpStatus() { + @Override + public void onResult(int i) { + applicationsManager.getApplications(new ApplicationsManager.CallbackApplications() { + @Override + public void onResult(int status, List applicationsList) { + applications = applicationsList; + notifyDataSetChanged(); + } + }); + } + }); + } + }); + } + } + catch (BboxNotFoundException e) { + e.toast(mContext); } } return view; diff --git a/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/BboxHolder.java b/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/BboxHolder.java index 80aba31..b1e40bb 100644 --- a/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/BboxHolder.java +++ b/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/BboxHolder.java @@ -3,6 +3,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.util.Log; +import android.widget.Toast; import fr.bouyguestelecom.tv.openapi.secondscreen.authenticate.IAuthCallback; import fr.bouyguestelecom.tv.openapi.secondscreen.bbox.Bbox; @@ -79,9 +80,9 @@ public void authenticate(String appId, String appSecret, IAuthCallback callback) * * @return the bbox. */ - public Bbox getBbox() { + public Bbox getBbox() throws BboxNotFoundException { if (mBbox == null) { - throw new RuntimeException("No bbox found! Check if a Miami bbox is present in the same network and if the service BboxApi is running."); + throw new BboxNotFoundException(); } return mBbox; } diff --git a/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/BboxNotFoundException.java b/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/BboxNotFoundException.java new file mode 100644 index 0000000..e74d769 --- /dev/null +++ b/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/BboxNotFoundException.java @@ -0,0 +1,19 @@ +package fr.bouyguestelecom.bboxlab.remotetemplate; + +import android.content.Context; +import android.widget.Toast; + +/** + * Created by tristan on 09/11/15. + */ +public class BboxNotFoundException extends Exception { + + public BboxNotFoundException() { + super("No bbox found! Check if a Miami bbox is present in the same network and if the service BboxApi is running."); + } + + public void toast (Context context) { + Toast toast = Toast.makeText(context, "No Bbox found. Please make sure the box is on the same network with the service BboxApi running.", Toast.LENGTH_LONG); + toast.show(); + } +} diff --git a/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/MyActivity.java b/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/MyActivity.java index 02161f5..9408175 100644 --- a/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/MyActivity.java +++ b/RemoteTemplate/app/src/main/java/fr/bouyguestelecom/bboxlab/remotetemplate/MyActivity.java @@ -90,8 +90,9 @@ public void onAuthResult(int code, String msg) displayWarningConnectivityMessage(msg); //Something wrong happen during authentication process return; } //Check the reason to know exactly why there is an error (problem with tokens means problem during connection with distant platform - //Authentication while problem with sessionId means problem with bbox connectivity check) - final Bbox bbox = BboxHolder.getInstance().getBbox(); //Starts almost everything, creates a BboxHolder that holds a Bbox + try { //Authentication while problem with sessionId means problem with bbox connectivity check) + final Bbox bbox = BboxHolder.getInstance().getBbox(); //Starts almost everything, creates a BboxHolder that holds a Bbox + if (bbox != null) { @@ -151,7 +152,10 @@ public void onConnect() } }); } - initAnymoteConnection(); //Pairing to a Bbox through anymote protocol + initAnymoteConnection(); + } + catch (BboxNotFoundException e) { + }//Pairing to a Bbox through anymote protocol } }; mNavigationDrawerFragment.setUp(R.id.navigation_drawer, @@ -336,12 +340,16 @@ private void initSetting(View rootView) final TextView showCrtIP = (TextView) rootView.findViewById(R.id.showCrtIP); editText.setText(preference.getString("bboxIP", "*.*.*.*")); - showCrtIP.setText(preference.getString("*.*.*.*", BboxHolder.getInstance().getBbox().getIp())); - save.setOnClickListener(new View.OnClickListener() - { + try { + showCrtIP.setText(preference.getString("*.*.*.*", BboxHolder.getInstance().getBbox().getIp())); + } + catch (BboxNotFoundException e) { + e.toast(this.getActivity()); + }; + + save.setOnClickListener(new View.OnClickListener() { @Override - public void onClick(View view) - { + public void onClick(View view) { SharedPreferences.Editor editor = preference.edit(); editor.putString("bboxIP", editText.getText().toString()); @@ -366,23 +374,27 @@ public void onClick(View view) private void initAppController(final View rootView) // App controller { - ApplicationsManager applicationsManager = BboxHolder.getInstance().getBbox() - .getApplicationsManager(); + try { + ApplicationsManager applicationsManager = BboxHolder.getInstance().getBbox() + .getApplicationsManager(); - applicationsManager.getApplications(new ApplicationsManager.CallbackApplications() // We are getting all the installed applications on the Bbox, and show them in a listView. - { - @Override - public void onResult(int status, List applications) + applicationsManager.getApplications(new ApplicationsManager.CallbackApplications() // We are getting all the installed applications on the Bbox, and show them in a listView. { - ApplicationAdapter applicationAdapter = new ApplicationAdapter(getActivity() - .getApplicationContext() - , applications, getActivity()); - ListView listView = (ListView) rootView - .findViewById(R.id.listView); - - listView.setAdapter(applicationAdapter); - } - }); + @Override + public void onResult(int status, List applications) { + ApplicationAdapter applicationAdapter = new ApplicationAdapter(getActivity() + .getApplicationContext() + , applications, getActivity()); + ListView listView = (ListView) rootView + .findViewById(R.id.listView); + + listView.setAdapter(applicationAdapter); + } + }); + } + catch (BboxNotFoundException e) { + e.toast(this.getActivity()); + } } private void initNotifications(final View rootView) // Notifications @@ -442,36 +454,40 @@ public void onClick(View view) private void initChanList(final View rootView) //Starts Channel List view, taking place on main view { - MediaManager mediaMng = BboxHolder.getInstance().getBbox().getMediaManager(); //Preparing fetching sequence; Locking target Bbox, Checking setted Bbox presence, Using the setted Bbox's Media fetching sequence + try { - mediaMng.getChannels(new MediaManager.CallbackChannels() //Asynchronous REST data fetching through callback - { //Anonymous channels' fetching function startpoint - @Override - public void onResult(int status, final List chans) //On receiving JSON data, fill the Channel List's fragment with - { - MediaAdapter mediaAdp = new MediaAdapter(getActivity() - .getApplicationContext(), chans, getActivity()); //Initialize the Channel List's main fragment - ListView lstView = (ListView) rootView.findViewById(R.id.listView); //Create the ListView and fill it with the corresponding xml fragment; This is where getView is used through override - final ToggleButton tglBtn = (ToggleButton) rootView.findViewById(R.id.toggleButton); + MediaManager mediaMng = BboxHolder.getInstance().getBbox().getMediaManager(); //Preparing fetching sequence; Locking target Bbox, Checking setted Bbox presence, Using the setted Bbox's Media fetching sequence - tglBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() + mediaMng.getChannels(new MediaManager.CallbackChannels() //Asynchronous REST data fetching through callback + { //Anonymous channels' fetching function startpoint + @Override + public void onResult(int status, final List chans) //On receiving JSON data, fill the Channel List's fragment with { - @Override - public void onCheckedChanged(CompoundButton btnView, boolean isChk) - { - String lstStr = String.valueOf(chans.size()); - - if (isChk) { - tglBtn.setTextOn("Channels: " + lstStr); - } else { - tglBtn.setTextOff("Channels: " + lstStr); + MediaAdapter mediaAdp = new MediaAdapter(getActivity() + .getApplicationContext(), chans, getActivity()); //Initialize the Channel List's main fragment + ListView lstView = (ListView) rootView.findViewById(R.id.listView); //Create the ListView and fill it with the corresponding xml fragment; This is where getView is used through override + final ToggleButton tglBtn = (ToggleButton) rootView.findViewById(R.id.toggleButton); + + tglBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton btnView, boolean isChk) { + String lstStr = String.valueOf(chans.size()); + + if (isChk) { + tglBtn.setTextOn("Channels: " + lstStr); + } else { + tglBtn.setTextOff("Channels: " + lstStr); + } } - } - }); - - lstView.setAdapter(mediaAdp); //Fill lstView with every "element_media.xml"'s sub-fragments - } - }); + }); + if (lstView != null) + lstView.setAdapter(mediaAdp); //Fill lstView with every "element_media.xml"'s sub-fragments + } + }); + } + catch (BboxNotFoundException e) { + e.toast(this.getActivity()); + } } }