Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / resource-container / examples / android / RCSampleClientApp / app / src / main / java / org / iotivity / service / sample / client / ResourceListItem.java
1 /******************************************************************
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  * <p>
4  * <p>
5  * <p>
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  * <p>
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * <p>
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  ******************************************************************/
18
19 package org.iotivity.service.sample.client;
20
21 import android.os.Handler;
22 import android.util.Log;
23
24 import org.iotivity.service.RcsException;
25 import org.iotivity.service.RcsResourceAttributes;
26 import org.iotivity.service.client.RcsRemoteResourceObject;
27
28 public class ResourceListItem {
29     private final String LOG_TAG = "[RCSampleClient] "
30             + this.getClass().getSimpleName();
31
32     public String mResourceUri;
33     public RcsRemoteResourceObject mResource;
34     public RemoteResourceGetListener mGetListener;
35     public RemoteResourceCacheListener mCacheListener;
36     public boolean mCacheStarted;
37
38     private Handler mHandler;
39
40     public ResourceListItem(String resourceUri, RcsRemoteResourceObject resource, Handler handler)
41     {
42         mResourceUri = resourceUri;
43         mResource = resource;
44
45         mGetListener = new RemoteResourceGetListener();
46         mCacheListener = new RemoteResourceCacheListener();
47         mCacheStarted = false;
48
49         mHandler = handler;
50     }
51
52     private class RemoteResourceGetListener implements RcsRemoteResourceObject.OnRemoteAttributesReceivedListener
53     {
54
55         @Override
56         public void onAttributesReceived(final RcsResourceAttributes rcsResourceAttributes, int eCode) {
57             Log.i(LOG_TAG, "onAttributesReceived -- " + mResourceUri);
58
59             new Thread(new Runnable() {
60
61                 @Override
62                 public void run() {
63                     try {
64                         String logMessage;
65                         logMessage = "-- " + mResourceUri + "\n";
66                         logMessage += Utils.resourceAttributes(rcsResourceAttributes);
67
68                         mHandler.obtainMessage(ContainerClientActivity.MSG_ID_ATTRIBUTE_RECEIVED, logMessage).sendToTarget();
69                     } catch (RcsException e) {
70                         e.printStackTrace();
71                     }
72                  }
73             }).run();
74         }
75     }
76
77     private class RemoteResourceCacheListener implements RcsRemoteResourceObject.OnCacheUpdatedListener
78     {
79
80         @Override
81         public void onCacheUpdated(final RcsResourceAttributes rcsResourceAttributes) {
82             Log.i(LOG_TAG, "onCacheUpdated -- " + mResourceUri);
83
84             new Thread(new Runnable() {
85
86                 @Override
87                 public void run() {
88                     try {
89                         String logMessage;
90                         logMessage = "-- " + mResourceUri + "\n";
91                         logMessage += Utils.resourceAttributes(rcsResourceAttributes);
92
93                         mHandler.obtainMessage(ContainerClientActivity.MSG_ID_CACHEDATA_RECEIVED, logMessage).sendToTarget();
94                     } catch (RcsException e) {
95                         e.printStackTrace();
96                     }
97                 }
98             }).run();
99         }
100     }
101 }