[iot-gateway/lighting-gateway.git] / client / AndroidClients / LightingController / src / com / lightingcontroller / Zigbee / ZigbeeNotification.java
diff --git a/client/AndroidClients/LightingController/src/com/lightingcontroller/Zigbee/ZigbeeNotification.java b/client/AndroidClients/LightingController/src/com/lightingcontroller/Zigbee/ZigbeeNotification.java
--- a/client/AndroidClients/LightingController/src/com/lightingcontroller/Zigbee/ZigbeeNotification.java
+++ b/client/AndroidClients/LightingController/src/com/lightingcontroller/Zigbee/ZigbeeNotification.java
-/**************************************************************************************************
- Filename: ZigBeeNotification.java
- Revised: $$
- Revision: $$
-
- Description: ZigBee Notification Class
-
- Copyright (C) {2012} Texas Instruments Incorporated - http://www.ti.com/
-
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
-
- Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-
- Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the
- distribution.
-
- Neither the name of Texas Instruments Incorporated nor the names of
- its contributors may be used to endorse or promote products derived
- from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
-**************************************************************************************************/
-
-package com.lightingcontroller.Zigbee;
-
-import java.util.ArrayList;
-
-import com.lightingcontroller.R;
-
-import android.app.Activity;
-import android.app.AlertDialog;
-import android.content.DialogInterface;
-import android.graphics.Color;
-import android.graphics.drawable.BitmapDrawable;
-import android.os.Handler;
-import android.util.AttributeSet;
-import android.view.Gravity;
-import android.view.LayoutInflater;
-import android.view.MotionEvent;
-import android.view.View;
-import android.view.View.OnClickListener;
-import android.view.View.OnTouchListener;
-import android.view.ViewGroup;
-import android.widget.ImageView;
-import android.widget.LinearLayout;
-import android.widget.PopupWindow;
-import android.widget.TextView;
-import android.widget.Toast;
-
-public class ZigbeeNotification {
-
- static Activity acty;
- static Handler handle = new Handler();
- static boolean alive;
- static PopupWindow popwindow = null;
-
- static ArrayList<String> notices = new ArrayList<String>();
-
- static int timeout = 5000;
-
- public static void init(Activity a, int time_out)
- {
- init(a);
- timeout = time_out;
- }
-
- public static void init(Activity a)
- {
- acty = a;
- if (notices.size() > 0)
- {
- for (int i = 0 ; i < notices.size() ; i++)
- {
- showNotificationOnScreen(notices.get(i));
- }
- for (int i = 0 ; i < notices.size() ; i++)
- {
- notices.remove(i);
- }
- }
- }
-
- public static void closing()
- {
- if (popwindow != null)
- {
- popwindow.dismiss();
- handle.removeCallbacks(closePopupTask);
- }
- popwindow = null;
- acty = null;
- }
-
- public static void showNotification(String text)
- {
- if (acty == null)
- {
- notices.add(text);
- }
- else
- {
- showNotificationOnScreen(text);
- }
- }
-
- public static void showNotificationOnScreen(final String text)
- {
- final TextView text1 = new TextView(acty);
- text1.setText(text);
- text1.setTextColor(Color.WHITE);
- text1.setTextSize(18);
- text1.setGravity(Gravity.CENTER);
-
- if (popwindow == null)
- {
- LayoutInflater inflater = (LayoutInflater) acty.getLayoutInflater();
- View layout = inflater.inflate(R.layout.popup,
- (ViewGroup) acty.findViewById(R.id.toast_layout_root));
-
- ((LinearLayout) layout.findViewById(R.id.popup_text_box)).addView(text1);
-
- popwindow = new PopupWindow(layout,400,100,false);
- popwindow.setAnimationStyle(R.style.Animation_Popup);
- popwindow.setBackgroundDrawable(new BitmapDrawable());
- popwindow.setOutsideTouchable(false);
- popwindow.setTouchable(true);
- popwindow.setTouchInterceptor(new OnTouchListener() {
- public boolean onTouch(View v, MotionEvent event) {
- handle.removeCallbacks(closePopupTask);
- handle.post(closePopupTask);
- return false;
- }
- });
- try{
- acty.runOnUiThread (new Runnable(){
- public void run() {
-// if (acty!=null && acty.findViewById(R.id.MainMenu_lay)!=null)
-// popwindow.showAtLocation(acty.findViewById(R.id.MainMenu_lay), Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 0);
-// else
-// notices.add(text);
- }
- });
- } catch (Exception e)
- {
- // Dagnabbit.
- popwindow = null;
- }
- }
- else
- {
-
- acty.runOnUiThread (new Runnable(){
- public void run() {
- LinearLayout l = (LinearLayout)popwindow.getContentView().findViewById(R.id.popup_text_box);
- if (l.getChildCount()>2)
- l.removeViewAt(0);
- l.addView(text1);
- popwindow.update();
- }
- });
- }
-
- handle.removeCallbacks(closePopupTask);
- handle.postDelayed(closePopupTask, timeout);
- //SoundManager.playSound(SoundManager.NOTIFY, 1);
- }
-
-
- private static Runnable closePopupTask = new Runnable() {
- public void run() {
- if (popwindow!=null)
- {
- popwindow.dismiss();
- popwindow = null;
- }
- }
- };
-
-
-}
+/**************************************************************************************************\r
+ Filename: ZigBeeNotification.java\r
+ Revised: $$\r
+ Revision: $$\r
+\r
+ Description: ZigBee Notification Class\r
+\r
+ Copyright (C) {2012} Texas Instruments Incorporated - http://www.ti.com/\r
+\r
+\r
+ Redistribution and use in source and binary forms, with or without\r
+ modification, are permitted provided that the following conditions\r
+ are met:\r
+\r
+ Redistributions of source code must retain the above copyright\r
+ notice, this list of conditions and the following disclaimer.\r
+\r
+ Redistributions in binary form must reproduce the above copyright\r
+ notice, this list of conditions and the following disclaimer in the\r
+ documentation and/or other materials provided with the\r
+ distribution.\r
+\r
+ Neither the name of Texas Instruments Incorporated nor the names of\r
+ its contributors may be used to endorse or promote products derived\r
+ from this software without specific prior written permission.\r
+\r
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ \r
+ **************************************************************************************************/\r
+\r
+package com.lightingcontroller.Zigbee;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import com.lightingcontroller.R;\r
+import com.lightingcontroller.zllMain;\r
+\r
+import android.app.Activity;\r
+import android.app.AlertDialog;\r
+import android.app.AlertDialog.Builder;\r
+import android.app.Dialog;\r
+import android.content.DialogInterface;\r
+import android.content.Intent;\r
+import android.graphics.Color;\r
+import android.graphics.drawable.BitmapDrawable;\r
+import android.os.Handler;\r
+import android.view.Gravity;\r
+import android.view.LayoutInflater;\r
+import android.view.MotionEvent;\r
+import android.view.View;\r
+import android.view.View.OnTouchListener;\r
+import android.view.ViewGroup;\r
+import android.widget.EditText;\r
+import android.widget.LinearLayout;\r
+import android.widget.PopupWindow;\r
+import android.widget.TextView;\r
+\r
+public class ZigbeeNotification {\r
+\r
+ private static Activity acty = null;\r
+ private static Dialog notificationDialog = null;\r
+ private static boolean deviceRemove = false;\r
+\r
+ static ArrayList<ZigbeeDevice> newDevNotifications = new ArrayList<ZigbeeDevice>();\r
+\r
+ static int timeout = 5000;\r
+ static int cnt = 0;\r
+ \r
+ public static void init(Activity a) {\r
+ acty = a;\r
+ }\r
+\r
+ public static void closing() {\r
+ closeDialog();\r
+ acty = null;\r
+ }\r
+\r
+ public static void showNewDeviceNotification(final ZigbeeDevice device) {\r
+ cnt++;\r
+ if(cnt > 1)\r
+ cnt = 0;\r
+ \r
+ if (acty != null) {\r
+ if(notificationDialog != null)\r
+ {\r
+ newDevNotifications.add(device);\r
+ }\r
+ else\r
+ {\r
+ showNewDeviceNotificationDialog(device);\r
+ }\r
+ }\r
+ else\r
+ {\r
+ \r
+ }\r
+ \r
+ while (!newDevNotifications.isEmpty()) {\r
+ if (acty != null) {\r
+ if(notificationDialog == null)\r
+ {\r
+ ZigbeeDevice notifyDevice = newDevNotifications.remove(0);\r
+ showNewDeviceNotificationDialog(notifyDevice);\r
+ }\r
+ }\r
+ }\r
+ \r
+/* \r
+ // restart activity to update device drop down \r
+ Intent intent = new Intent();\r
+ intent.setClass(acty, acty.getClass());\r
+ acty.finish();\r
+ acty.startActivity(intent);\r
+*/\r
+ }\r
+\r
+ public static void showNewDeviceNotificationDialog(\r
+ final ZigbeeDevice device) {\r
+ // close any existing notification\r
+ closeDialog();\r
+ \r
+ //put device into identify\r
+ ZigbeeAssistant.setDeviceState(device,true);\r
+ ZigbeeAssistant.setDeviceLevel(device,0xFF);\r
+ ZigbeeAssistant.IdentifyDevice(device,(short) 600);\r
+ \r
+ String Title = "New Device Found";\r
+ String Msg = "Please Enter the name of the device";\r
+ final EditText t = new EditText(acty);\r
+ t.setText(device.Name);\r
+\r
+ final AlertDialog.Builder builder = new AlertDialog.Builder(acty);\r
+ builder.setTitle(Title)\r
+ .setMessage(Msg)\r
+ .setView(t)\r
+ .setPositiveButton("OK", \r
+ new DialogInterface.OnClickListener()\r
+ {\r
+ public void onClick(DialogInterface dialoginterface,int i){ \r
+ ZigbeeAssistant.setDeviceName(device.Name, t.getText().toString()); \r
+ zllMain.setCurrentDevice(device);\r
+ closeDialog();\r
+ //stop device identifying\r
+ ZigbeeAssistant.IdentifyDevice(device,(short) 0);\r
+ }\r
+ });\r
+ \r
+\r
+ try {\r
+ acty.runOnUiThread(new Runnable() {\r
+ public void run() {\r
+ if (acty != null) {\r
+ // if(acty.hasWindowFocus())\r
+ // {\r
+ notificationDialog = builder.create();\r
+ notificationDialog.show();\r
+ // }\r
+ }\r
+ }\r
+ });\r
+ } catch (Exception e) {\r
+\r
+ }\r
+ }\r
+\r
+ public static void showRemoveDeviceNotification(String deviceStr,\r
+ final int timeout) {\r
+ if (acty != null) {\r
+ // close any existing notification\r
+ closeDialog();\r
+\r
+ final AlertDialog.Builder builder = new AlertDialog.Builder(acty);\r
+ builder.setTitle("ZigBee Notification")\r
+ .setCancelable(false)\r
+ .setMessage("Device Removed: " + deviceStr)\r
+ .setPositiveButton("Ok",\r
+ new DialogInterface.OnClickListener() {\r
+ public void onClick(DialogInterface dialog,\r
+ int id) {\r
+ // restart activity to update device drop\r
+ // down\r
+ Intent intent = new Intent();\r
+ intent.setClass(acty, acty.getClass());\r
+ acty.finish();\r
+ acty.startActivity(intent);\r
+ closeDialog();\r
+ }\r
+ });\r
+\r
+ try {\r
+ acty.runOnUiThread(new Runnable() {\r
+ public void run() {\r
+ if (acty != null) {\r
+ // if(acty.hasWindowFocus())\r
+ // {\r
+ notificationDialog = builder.create();\r
+ notificationDialog.show();\r
+ // }\r
+ }\r
+ }\r
+ });\r
+ } catch (Exception e) {\r
+\r
+ }\r
+\r
+ new Thread(new Runnable() {\r
+ public void run() {\r
+ try {\r
+ Thread.sleep(timeout);\r
+ } catch (InterruptedException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ closeDialog();\r
+ }\r
+ }).start();\r
+\r
+ }\r
+ }\r
+\r
+ public static void showGeneralNotification(String notification,\r
+ final int timeout) {\r
+ if (acty != null) {\r
+ // close any existing notification\r
+ closeDialog();\r
+\r
+ final AlertDialog.Builder builder = new AlertDialog.Builder(acty);\r
+ builder.setTitle("ZigBee Notification").setCancelable(false)\r
+ .setMessage(notification);\r
+\r
+ try {\r
+ acty.runOnUiThread(new Runnable() {\r
+ public void run() {\r
+ if (acty != null) {\r
+ // if(acty.hasWindowFocus())\r
+ // {\r
+ notificationDialog = builder.create();\r
+ notificationDialog.show();\r
+ // }\r
+ }\r
+ }\r
+ });\r
+ } catch (Exception e) {\r
+\r
+ }\r
+\r
+ new Thread(new Runnable() {\r
+ public void run() {\r
+ try {\r
+ Thread.sleep(timeout);\r
+ } catch (InterruptedException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ }\r
+ closeDialog();\r
+ }\r
+ }).start();\r
+ }\r
+ }\r
+\r
+ private static void closeDialog() {\r
+ if (notificationDialog != null) {\r
+ notificationDialog.cancel();\r
+ }\r
+ notificationDialog = null;\r
+ }\r
+}\r