Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / protocol-plugin / plugin-manager / src / Android / src / org / iotivity / service / ppm / MainActivity.java
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /// @file MainActivity.java
22
23 package org.iotivity.service.ppm;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.iotivity.base.ModeType;
28 import org.iotivity.base.OcConnectivityType;
29 import org.iotivity.base.OcException;
30 import org.iotivity.base.OcPlatform;
31 import org.iotivity.base.OcRepresentation;
32 import org.iotivity.base.OcResource;
33 import org.iotivity.base.PlatformConfig;
34 import org.iotivity.base.QualityOfService;
35 import org.iotivity.base.ServiceType;
36 import org.iotivity.service.ppm.R;
37
38 import android.app.Activity;
39 import android.content.Context;
40 import android.os.Bundle;
41 import android.os.Handler;
42 import android.util.Log;
43 import android.view.View;
44 import android.widget.Button;
45 import android.widget.TextView;
46 import android.widget.Toast;
47 import android.widget.ToggleButton;
48
49 public class MainActivity extends Activity implements
50         android.widget.NumberPicker.OnValueChangeListener {
51     class S_demo_device {
52         public String m_name;
53         public String m_power;
54         public int    m_bright;
55         public int    m_color;
56         public String m_uri;
57     }
58
59     static S_demo_device               belkinplug;
60     static S_demo_device               gearplug;
61     static S_demo_device               hueplug;
62     static OcResource                  belkinResource;
63     static OcResource                  gearResource;
64     static OcResource                  hueResource;
65     static TextView                    belkintv;
66     static TextView                    geartv;
67     static TextView                    huetv;
68     static ToggleButton                Hue;
69     static android.widget.NumberPicker hue_color;
70     static Activity                    mActivity;
71
72     PluginManager                      m_pm;
73
74     Button                             belkinstart;
75     Button                             belkingetPlugins;
76     Button                             belkingetstate;
77     Button                             belkinstop;
78     Button                             belkinrescan;
79     Button                             Belkin;
80
81     Button                             gearstart;
82     Button                             geargetPlugins;
83     Button                             geargetstate;
84     Button                             gearstop;
85     Button                             gearrescan;
86     Button                             Gear;
87
88     Button                             huestart;
89     Button                             huegetPlugins;
90     Button                             huegetstate;
91     Button                             huestop;
92     Button                             huerescan;
93
94     Context                            ctx;
95
96     String                             name;
97     String                             key;
98     String                             state;
99     String                             id;
100     Plugin[]                           user_plugin;
101     Map<Integer, Integer>              onValueChangefinalVal    = new HashMap<Integer, Integer>();
102     Map<Integer, Boolean>              onValueChangeThreadStart = new HashMap<Integer, Boolean>();
103     String[]                           np_h                     = new String[11];
104
105     @Override
106     protected void onCreate(Bundle savedInstanceState) {
107
108         super.onCreate(savedInstanceState);
109         ctx = getApplicationContext();
110         m_pm = new PluginManager(ctx);
111
112         setContentView(R.layout.activity_main);
113         mActivity = this;
114         onValueChangeThreadStart.put(R.id.np_hue_color, false);
115         belkinplug = new S_demo_device();
116         gearplug = new S_demo_device();
117         hueplug = new S_demo_device();
118
119         for (int i = 0; i < np_h.length; i++) {
120             np_h[i] = Integer.toString(i);
121         }
122
123         hue_color = (android.widget.NumberPicker) findViewById(R.id.np_hue_color);
124         hue_color.setMaxValue(10);
125         hue_color.setMinValue(0);
126         hue_color.setDisplayedValues(np_h);
127         hue_color.setFocusable(true);
128         hue_color.setFocusableInTouchMode(true);
129         hue_color
130                 .setDescendantFocusability(android.widget.NumberPicker.FOCUS_BLOCK_DESCENDANTS);
131         hue_color.setOnValueChangedListener(this);
132
133         PlatformConfig cfg = new PlatformConfig(this, ServiceType.IN_PROC,
134                 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
135
136         OcPlatform.Configure(cfg);
137
138         try {
139             FoundResource foundResource = new FoundResource();
140             OcPlatform.findResource("", OcPlatform.WELL_KNOWN_QUERY,
141                     OcConnectivityType.ALL, foundResource);
142         } catch (Exception e) {
143             Log.e("Felix", "Exception : " + e);
144         }
145
146         Belkin = (Button) findViewById(R.id.btn_belkin);
147         Belkin.setOnClickListener(new Button.OnClickListener() {
148             public void onClick(View v) {
149                 Log.i("Felix", "Belkin button click listener");
150                 OcRepresentation rep = new OcRepresentation();
151                 try {
152                     if (belkinplug.m_power == null) {
153                         Log.i("Felix", "m_power is null");
154                         belkinplug.m_power = "on";
155                         rep.setValue("power", "on");
156                     }
157                     if (belkinplug.m_power.equals("on")) {
158                         Toast.makeText(getApplicationContext(), "Off",
159                                 Toast.LENGTH_SHORT).show();
160                         Log.i("Felix", "belkin wemo off");
161                         rep.setValue("power", "off");
162                     } else if (belkinplug.m_power.equals("off")) {
163                         Toast.makeText(getApplicationContext(), "On",
164                                 Toast.LENGTH_SHORT).show();
165                         Log.i("Felix", "belkin wemo on");
166                         rep.setValue("power", "on");
167                     } else {
168                         rep.setValue("power", "on");
169                     }
170                     rep.setValue("name", "belkin");
171                     rep.setValue("uri", "/a/wemo");
172                     rep.setValue("brightness", 0);
173                     rep.setValue("color", 0);
174                 } catch (OcException e) {
175                     Log.e("Felix", e.getMessage());
176                 }
177                 OnPutBelkinplug onPut = new OnPutBelkinplug();
178                 if (belkinResource != null) {
179                     try {
180                         belkinResource.put(rep, new HashMap<String, String>(),
181                                 onPut);
182                     } catch (OcException e) {
183                         e.printStackTrace();
184                     }
185                 } else {
186                     Toast.makeText(getApplicationContext(), "Belkinplug null",
187                             Toast.LENGTH_SHORT).show();
188                 }
189             }
190         });
191
192         belkinstart = (Button) findViewById(R.id.Button01);
193         belkinstart.setOnClickListener(new Button.OnClickListener() {
194             public void onClick(View v) {
195                 Log.i("Felix", "start button click listener");
196                 m_pm.startPlugins("ResourceType", "device.smartplug");
197
198                 Handler handler = new Handler();
199                 handler.postDelayed(new Runnable() {
200                     @Override
201                     public void run() {
202                         Log.i("Felix", "run called!!!");
203                         FoundResource foundResource = new FoundResource();
204                         try {
205                             OcPlatform
206                                     .findResource(
207                                             "", OcPlatform.WELL_KNOWN_QUERY + "?rt=" + "device.smartplug",
208                                             OcConnectivityType.ALL, foundResource);
209                         } catch (OcException e) {
210                             e.printStackTrace();
211                         }
212                     }
213                 }, 3000);
214             }
215         });
216
217         belkinstop = (Button) findViewById(R.id.Button02);
218         belkinstop.setOnClickListener(new Button.OnClickListener() {
219             public void onClick(View v) {
220                 Log.i("Felix", "stop button click listener");
221                 m_pm.stopPlugins("ResourceType", "device.smartplug");
222                 belkinResource = null;
223                 try {
224                     java.lang.Thread.sleep(2000);
225                 } catch (InterruptedException e) {
226                     e.printStackTrace();
227                 }
228             }
229         });
230
231         belkingetPlugins = (Button) findViewById(R.id.Button03);
232         belkingetPlugins.setOnClickListener(new Button.OnClickListener() {
233             public void onClick(View v) {
234                 Log.i("Felix", "getPlugins button click listener");
235                 user_plugin = m_pm.getPlugins();
236                 // key = "name";
237                 state = "";
238                 id = "";
239
240                 for (int i = 0; i < user_plugin.length; i++) {
241                     System.out.println("value name " + user_plugin[i].getName());
242                     System.out.println("value id " + user_plugin[i].getID());
243                     // name = user_plugin[i].getID();
244                 }
245             }
246         });
247
248         belkingetPlugins = (Button) findViewById(R.id.Button04);
249         belkingetPlugins.setOnClickListener(new Button.OnClickListener() {
250             public void onClick(View v) {
251                 Log.i("Felix", "getState click listener");
252                 state = m_pm.getState("wemo");
253                 if (state == "")
254                     state = "null";
255                 System.out.println("state: " + state);
256                 try {
257                     Thread.sleep(2000);
258                 } catch (InterruptedException e) {
259                     e.printStackTrace();
260                 }
261             }
262         });
263
264         belkinrescan = (Button) findViewById(R.id.Button05);
265         belkinrescan.setOnClickListener(new Button.OnClickListener() {
266             public void onClick(View v) {
267                 Log.i("Felix", "Rescan Plugin click listener");
268                 m_pm.rescanPlugin();
269                 try {
270                     Thread.sleep(2000);
271                 } catch (InterruptedException e) {
272                     e.printStackTrace();
273                 }
274             }
275         });
276
277         Gear = (Button) findViewById(R.id.btn_gear);
278         Gear.setOnClickListener(new Button.OnClickListener() {
279             public void onClick(View v) {
280                 Log.i("Felix", "Gear button click listener");
281                 OcRepresentation rep = new OcRepresentation();
282                 
283                 try{   
284                     rep.setValue("power", "Happy New Year!");
285                     rep.setValue("name", "gear");
286                     rep.setValue("uri", "/a/galaxy/gear");
287                     rep.setValue("brightness", 0);
288                     rep.setValue("color", 0);
289                 } catch (OcException e) {
290                     Log.e("Felix", e.getMessage());
291                 }
292                 
293                 if (gearResource != null) {
294                     Toast.makeText(getApplicationContext(),
295                             "Send Noti. to Gear", Toast.LENGTH_SHORT).show();
296                     try {
297                         OnPutGear onPut = new OnPutGear();
298                         gearResource.put(rep, new HashMap<String, String>(),
299                                 onPut);
300                     } catch (OcException e) {
301                         e.printStackTrace();
302                     }
303                 } else {
304                     Toast.makeText(getApplicationContext(), "Gear is null",
305                             Toast.LENGTH_SHORT).show();
306                 }
307             }
308         });
309
310         gearstart = (Button) findViewById(R.id.Button06);
311         gearstart.setOnClickListener(new Button.OnClickListener() {
312             public void onClick(View v) {
313                 Log.i("Felix", "start button click listener");
314                 m_pm.startPlugins("ResourceType", "device.notify");
315
316                 Handler handler = new Handler();
317                 handler.postDelayed(new Runnable() {
318                     @Override
319                     public void run() {
320                         Log.i("Felix", "run called!!!");
321                         FoundResource foundResource = new FoundResource();
322                         try {
323                             OcPlatform
324                             .findResource(
325                                     "", OcPlatform.WELL_KNOWN_QUERY + "?rt=" + "device.notify",
326                                     OcConnectivityType.ALL, foundResource);
327                         } catch (OcException e) {
328                             e.printStackTrace();
329                         }
330                     }
331                 }, 3000);
332             }
333         });
334
335         gearstop = (Button) findViewById(R.id.Button07);
336         gearstop.setOnClickListener(new Button.OnClickListener() {
337             public void onClick(View v) {
338                 Log.i("Felix", "stop button click listener");
339                 m_pm.stopPlugins("ResourceType", "device.notify");
340                 gearResource = null;
341                 try {
342                     java.lang.Thread.sleep(2000);
343                 } catch (InterruptedException e) {
344                     e.printStackTrace();
345                 }
346             }
347         });
348
349         geargetPlugins = (Button) findViewById(R.id.Button08);
350         geargetPlugins.setOnClickListener(new Button.OnClickListener() {
351             public void onClick(View v) {
352                 Log.i("Felix", "getPlugins button click listener");
353                 user_plugin = m_pm.getPlugins();
354                 state = "";
355                 id = "";
356
357                 for (int i = 0; i < user_plugin.length; i++) {
358                     System.out.println("value name " + user_plugin[i].getName());
359                     System.out.println("value id " + user_plugin[i].getID());
360                     name = user_plugin[i].getID();
361                 }
362             }
363         });
364
365         geargetPlugins = (Button) findViewById(R.id.Button09);
366         geargetPlugins.setOnClickListener(new Button.OnClickListener() {
367             public void onClick(View v) {
368                 Log.i("Felix", "getState click listener");
369                 state = m_pm.getState("gearnoti");
370                 if (state == "")
371                     state = "null";
372                 System.out.println("state: " + state);
373                 try {
374                     Thread.sleep(2000);
375                 } catch (InterruptedException e) {
376                     e.printStackTrace();
377                 }
378             }
379         });
380
381         gearrescan = (Button) findViewById(R.id.Button10);
382         gearrescan.setOnClickListener(new Button.OnClickListener() {
383             public void onClick(View v) {
384                 Log.i("Felix", "Rescan Plugin click listener");
385                 m_pm.rescanPlugin();
386                 try {
387                     Thread.sleep(2000);
388                 } catch (InterruptedException e) {
389                     e.printStackTrace();
390                 }
391             }
392         });
393
394         Hue = (ToggleButton) findViewById(R.id.tbtn_hue_power);
395         Hue.setOnClickListener(new ToggleButton.OnClickListener() {
396             public void onClick(View v) {
397                 Log.i("Felix", "Hue button click listener");
398                 OcRepresentation rep = new OcRepresentation();
399                 ToggleButton t = (ToggleButton) v;
400                 if (t.isChecked()) {
401                     Toast.makeText(getApplicationContext(), "Hue ON",
402                             Toast.LENGTH_SHORT).show();
403                     if (hueplug.m_bright == 0) {
404                         Log.e("Felix", "hueplug m_bright is 0");
405                         hueplug.m_bright = 128;
406                     }
407                     try {
408                         rep.setValue("power", "on");
409                         rep.setValue("name", "hue");
410                         rep.setValue("uri", "/a/huebulb");
411                         rep.setValue("brightness", hueplug.m_bright);
412                         rep.setValue("color", hueplug.m_color);
413                     } catch (OcException e) {
414                         Log.e("Felix", e.getMessage());
415                     }
416
417                     OnPutHuebulb onPut = new OnPutHuebulb();
418                     if (hueResource != null) {
419                         try {
420                             hueResource.put(rep, new HashMap<String, String>(),
421                                     onPut);
422                         } catch (OcException e) {
423                             e.printStackTrace();
424                         }
425                     } else {
426                         Toast.makeText(getApplicationContext(),
427                                 "HueResource null", Toast.LENGTH_SHORT).show();
428                     }
429                 } else {
430                     Toast.makeText(getApplicationContext(), "Hue OFF",
431                             Toast.LENGTH_SHORT).show();
432                     if (hueplug.m_bright == 0) {
433                         Log.e("Felix", "hueplug m_bright is 0");
434                         hueplug.m_bright = 128;
435                     }
436                     try {
437                         rep.setValue("power", "off");
438                         rep.setValue("name", "hue");
439                         rep.setValue("uri", "/a/huebulb");
440                         rep.setValue("brightness", hueplug.m_bright);
441                         rep.setValue("color", hueplug.m_color);
442                     } catch (OcException e) {
443                         Log.e("Felix", e.getMessage());
444                     }
445
446                     OnPutHuebulb onPut = new OnPutHuebulb();
447
448                     if (hueResource != null) {
449                         try {
450                             hueResource.put(rep, new HashMap<String, String>(),
451                                     onPut);
452                         } catch (OcException e) {
453                             e.printStackTrace();
454                         }
455                     } else {
456                         Toast.makeText(getApplicationContext(),
457                                 "HueResource null", Toast.LENGTH_SHORT).show();
458                     }
459                 }
460             }
461         });
462
463         huestart = (Button) findViewById(R.id.Button11);
464         huestart.setOnClickListener(new Button.OnClickListener() {
465             public void onClick(View v) {
466                 Log.i("Felix", "start button click listener");
467                 m_pm.startPlugins("ResourceType", "device.light");
468
469                 Handler handler = new Handler();
470                 handler.postDelayed(new Runnable() {
471                     @Override
472                     public void run() {
473                         Log.i("Felix", "run called!!!");
474                         FoundResource foundResource = new FoundResource();
475                         try {
476                             OcPlatform
477                             .findResource(
478                                     "", OcPlatform.WELL_KNOWN_QUERY + "?rt=" + "device.light",
479                                     OcConnectivityType.ALL, foundResource);
480                         } catch (OcException e) {
481                             e.printStackTrace();
482                         }
483                     }
484                 }, 3000);
485             }
486         });
487
488         huestop = (Button) findViewById(R.id.Button12);
489         huestop.setOnClickListener(new Button.OnClickListener() {
490             public void onClick(View v) {
491                 Log.i("Felix", "stop button click listener");
492                 m_pm.stopPlugins("ResourceType", "device.light");
493                 hueResource = null;
494                 try {
495                     java.lang.Thread.sleep(2000);
496                 } catch (InterruptedException e) {
497                     e.printStackTrace();
498                 }
499             }
500         });
501
502         huegetPlugins = (Button) findViewById(R.id.Button13);
503         huegetPlugins.setOnClickListener(new Button.OnClickListener() {
504             public void onClick(View v) {
505                 Log.i("Felix", "getPlugins button click listener");
506                 user_plugin = m_pm.getPlugins();
507                 key = "name";
508                 state = "";
509                 id = "";
510
511                 for (int i = 0; i < user_plugin.length; i++) {
512                     System.out.println("value name " + user_plugin[i].getName());
513                     System.out.println("value id " + user_plugin[i].getID());
514                     name = user_plugin[i].getID();
515                 }
516             }
517         });
518
519         huegetPlugins = (Button) findViewById(R.id.Button14);
520         huegetPlugins.setOnClickListener(new Button.OnClickListener() {
521             public void onClick(View v) {
522                 Log.i("Felix", "getState click listener");
523                 state = m_pm.getState("hue");
524                 if (state == "")
525                     state = "null";
526                 System.out.println("state: " + state);
527                 try {
528                     Thread.sleep(2000);
529                 } catch (InterruptedException e) {
530                     e.printStackTrace();
531                 }
532             }
533         });
534
535         huerescan = (Button) findViewById(R.id.Button15);
536         huerescan.setOnClickListener(new Button.OnClickListener() {
537             public void onClick(View v) {
538                 Log.i("Felix", "Rescan Plugin click listener");
539                 m_pm.rescanPlugin();
540                 try {
541                     Thread.sleep(2000);
542                 } catch (InterruptedException e) {
543                     e.printStackTrace();
544                 }
545             }
546         });
547     }
548
549     static public void updateConnectStatus(String device, boolean status) {
550         if (device.equals("belkinplug")) {
551             if (status) {
552                 Log.i("Felix", "belkingplug status green");
553                 Toast.makeText(mActivity.getApplicationContext(),
554                         "Belkin Connected", Toast.LENGTH_SHORT).show();
555             } else {
556                 Log.i("Felix", "belkingplug status gray");
557                 Toast.makeText(mActivity.getApplicationContext(),
558                         "Belkin Disonnected", Toast.LENGTH_SHORT).show();
559             }
560         } else if (device.equals("gear")) {
561             if (status) {
562                 Log.i("Felix", "gear status green");
563                 Toast.makeText(mActivity.getApplicationContext(),
564                         "Gear Connected", Toast.LENGTH_SHORT).show();
565             } else {
566                 Log.i("Felix", "gear status gray");
567                 Toast.makeText(mActivity.getApplicationContext(),
568                         "Gear Disconnected", Toast.LENGTH_SHORT).show();
569             }
570         } else if (device.equals("huebulb")) {
571             if (status) {
572                 Log.i("Felix", "huebulb status green");
573                 Toast.makeText(mActivity.getApplicationContext(),
574                         "Hue Connected", Toast.LENGTH_SHORT).show();
575             } else {
576                 Log.i("Felix", "huebulb status gray");
577                 Toast.makeText(mActivity.getApplicationContext(),
578                         "Hue Disconnected", Toast.LENGTH_SHORT).show();
579             }
580
581             if (hueplug.m_power.equals("on")) {
582                 Hue.setChecked(true);
583             } else {
584                 Hue.setChecked(false);
585             }
586         }
587     }
588
589     static public void updateBelkinStatus() {
590         updateConnectStatus("belkinplug", true);
591     }
592
593     static public void updateGearStatus() {
594         updateConnectStatus("gear", true);
595     }
596
597     static public void updateHuebulbStatus() {
598         updateConnectStatus("huebulb", true);
599     }
600
601     @Override
602     protected void onResume() {
603         Log.i("Felix", "onResume()");
604         super.onResume();
605     }
606
607     @Override
608     protected void onPause() {
609         Log.i("Felix", "onPause()");
610         super.onPause();
611         finish();
612     }
613
614     @Override
615     protected void onStop() {
616         Log.i("Felix", "onStop()");
617         super.onStop();
618     }
619
620     @Override
621     protected void onDestroy() {
622         Log.i("Felix", "onDestroy()");
623         System.exit(1);
624         super.onDestroy();
625     }
626
627     private class ValueChangeThread extends Thread {
628         int idx;
629
630         public ValueChangeThread(int idx) {
631             this.idx = idx;
632         }
633
634         public void run() {
635             onValueChangeThreadStart.put(idx, true);
636             try {
637                 sleep(1000, 0);
638             } catch (Exception e) {
639                 Log.i("Felix", "waitForFinal exception : " + e);
640             }
641             Log.i("Felix", "Final Value for NUMBERPICKER[" + idx + "] : "
642                     + onValueChangefinalVal.get(idx));
643
644             if (idx == R.id.np_hue_color) {
645                 hueplug.m_color = 6300 * onValueChangefinalVal.get(idx);
646                 Log.i("Felix", "m_color = " + hueplug.m_color);
647
648                 OcRepresentation rep = new OcRepresentation();
649                 if (hueplug.m_power == null) {
650                     hueplug.m_power = "on";
651                 }
652                 if (hueplug.m_bright == 0) {
653                     hueplug.m_bright = 128;
654                 }
655                 try{
656                     rep.setValue("power", hueplug.m_power);
657                     rep.setValue("name", "huebulb");
658                     rep.setValue("uri", "/a/huebulb");
659                     rep.setValue("brightness", hueplug.m_bright = 180);
660                     rep.setValue("color", hueplug.m_color);
661                 } catch (OcException e) {
662                     Log.e("Felix", e.getMessage());
663                 }
664
665                 OnPutHuebulb onPut = new OnPutHuebulb();
666                 if (hueResource != null) {
667                     Log.i("Felix", "huebulbResource is not null");
668                     try {
669                         hueResource.put(rep, new HashMap<String, String>(),
670                                 onPut);
671                     } catch (OcException e) {
672                         e.printStackTrace();
673                     }
674                 } else {
675                     Log.i("Felix", "huebulbResource is null");
676                 }
677             }
678             onValueChangeThreadStart.put(idx, false);
679         }
680     }
681
682     public void onValueChange(android.widget.NumberPicker picker, int oldVal,
683             int newVal) {
684         final int pickerIdx = picker.getId();
685
686         Thread waitForFinal = new ValueChangeThread(pickerIdx);
687         if (onValueChangeThreadStart.get(pickerIdx) == false)
688             waitForFinal.start();
689
690         onValueChangefinalVal.put(pickerIdx, newVal);
691     }
692 }