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