Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / android / OCClient / src / com / oc / ub / sample / OCClient.java
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 package com.oc.ub.sample;
7
8 import com.oc.OCDiscovery;
9 import com.oc.OCObserver;
10 import com.oc.OCResource;
11 import com.oc.OCResourceResultHandler;
12 import com.oc.OCServer;
13 import com.oc.ub.sampleclient.R;
14
15 import android.app.Activity;
16 import android.app.Fragment;
17 import android.os.Bundle;
18 import android.view.LayoutInflater;
19 import android.view.Menu;
20 import android.view.MenuItem;
21 import android.view.View;
22 import android.view.ViewGroup;
23
24 public class OCClient extends Activity {
25   private static String TAG = "OCCLIENT";
26   private OCServer ocServer = null;
27   private OCDiscovery ocDiscovery = null;
28
29   @Override
30   protected void onCreate(Bundle savedInstanceState) {
31     super.onCreate(savedInstanceState);
32     setContentView(R.layout.activity_occlient);
33
34     if (savedInstanceState == null) {
35       getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment())
36           .commit();
37     }
38
39     ocServer = new OCServer();
40     ocServer.start();
41     ocDiscovery = ocServer.getDiscoverySingleton();
42
43     /*
44      * APPLICATION NOTE: All callbacks will come through on a new thread and will not interfere with
45      * the UI thread or the Framework Thread (i.e. Networking Thread) which means that you need to
46      * use a Handler() to post back to the UI.
47      */
48     ocDiscovery.findResourceByType(null, null, new OCResourceResultHandler() {
49       @Override
50       public void onResourcesFound(String serviceURL, OCResource[] resources) {
51         // NOTE: This means that a service is reporting back zero or more resources
52         // During the callback, lets check to see if the resource supports the
53         // light control interface and turn the light on and then subscribe to any changes in the
54         // light's power state.
55         for (OCResource resource : resources) {
56           try {
57             LightControl control = resource.getInterface(LightControl.class);
58
59             if (control != null) {
60               control.setPowered(true);
61               control.isPowered();
62               /*
63                * APPLICATION NOTE: All callbacks will come through on a new thread and will not
64                * interfere with the UI thread or the Framework Thread (i.e. Networking Thread) which
65                * means that you need to use a Handler() to post back to the UI.
66                */
67               control.addPoweredObserver(new OCObserver<Boolean>() {
68                 @Override
69                 public void onChanged(Boolean data) {
70                   // TODO: Tell the UI! The state of the Light has changed.
71
72                 }
73
74                 @Override
75                 public void onFailed(Throwable throwable) {
76                   // TODO: Tell the UI! We are no longer subscribed to events for the Light's power
77                   // state.
78                 }
79               });
80             }
81           } catch (Exception exception) {
82             // TODO: Ooops
83           }
84         }
85
86       }
87
88       @Override
89       public void onFailed(Throwable throwable) {
90         // TODO: Handle the error. The request to find resources has failed.
91       }
92
93       @Override
94       public void onCompleted() {
95         // Nothing to do here... we are just done finding resources is all.
96       }
97     });
98   }
99
100   @Override
101   public boolean onCreateOptionsMenu(Menu menu) {
102
103     // Inflate the menu; this adds items to the action bar if it is present.
104     getMenuInflater().inflate(R.menu.occlient, menu);
105     return true;
106   }
107
108   @Override
109   public boolean onOptionsItemSelected(MenuItem item) {
110     // Handle action bar item clicks here. The action bar will automatically handle clicks on the
111     // Home/Up button, so long as you specify a parent activity in AndroidManifest.xml.
112     int id = item.getItemId();
113     if (id == R.id.action_settings) {
114       return true;
115     }
116     return super.onOptionsItemSelected(item);
117   }
118
119   @Override
120   protected void onDestroy() {
121     super.onDestroy();
122
123     ocServer.stop();
124   }
125
126   /**
127    * A placeholder fragment containing a simple view.
128    */
129   public static class PlaceholderFragment extends Fragment {
130
131     public PlaceholderFragment() {}
132
133     @Override
134     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
135       View rootView = inflater.inflate(R.layout.fragment_occlient, container, false);
136       return rootView;
137     }
138   }
139 }