1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.example.testplugin;
18
19import android.app.Activity;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.ServiceConnection;
24import android.os.Bundle;
25import android.os.IBinder;
26import android.os.RemoteException;
27import android.util.Log;
28
29import com.android.omadm.plugin.DmtData;
30import com.android.omadm.plugin.DmtException;
31import com.android.omadm.plugin.IDMClientService;
32
33import java.util.Arrays;
34import java.util.Map;
35
36public class GetDMTreeActivity extends Activity {
37    static final String TAG = "GetDMTreeActivity";
38
39    // Binder interface to client service
40    IDMClientService mDMClientService;
41
42    // Service connector object to attach/detach binder interface
43    private final DMClientServiceConnector mDMServiceConnector = new DMClientServiceConnector();
44
45    private class DMClientServiceConnector implements ServiceConnection {
46        // package local constructor (called by outer class)
47        DMClientServiceConnector() {}
48
49        @Override
50        public void onServiceConnected(ComponentName name, IBinder service) {
51            Log.d(TAG, "onServiceConnected for " + name + " service " + service);
52            mDMClientService = IDMClientService.Stub.asInterface(service);
53
54            try {
55                Log.d(TAG, "getting DM tree and dumping to log");
56
57                DmtData swVersion = mDMClientService.getDMTree("./DevDetail/SwV", false);
58                Log.d(TAG, "node \"./DevDetail/SwV\" type: " + swVersion.getType() + " value: "
59                        + swVersion.getString());
60
61                DmtData treeRoot = mDMClientService.getDMTree(".", true);
62                Log.d(TAG, "tree root: type " + treeRoot.getType() + ": " + treeRoot.getString());
63                if (treeRoot.getType() == DmtData.NODE) {
64                    printChildren(treeRoot, "    ");
65                }
66
67                DmtData wifiNode = mDMClientService.getDMTree("./Wi-Fi", true);
68                Log.d(TAG, "HotSpot wifi node root is " + wifiNode);
69                if (wifiNode.getType() == DmtData.NODE) {
70                    printChildren(wifiNode, "    ");
71                }
72            } catch (RemoteException e) {
73                Log.e(TAG, "remote exception", e);
74            }
75
76            Log.d(TAG, "finishing activity");
77            finish();
78        }
79
80        private void printChildren(DmtData node, String indent) {
81            try {
82                Map<String, DmtData> childNodeMap = node.getChildNodeMap();
83                for (Map.Entry<String, DmtData> entry : childNodeMap.entrySet()) {
84                    DmtData value = entry.getValue();
85                    Log.d(TAG, indent + " name: " + entry.getKey() + " type: " + value.getType()
86                            + ": " + value.getString());
87                    if (value.getType() == DmtData.NODE) {
88                        printChildren(value, indent + "    ");
89                    }
90                }
91            } catch (DmtException e) {
92                Log.e(TAG, "DmtException", e);
93            }
94        }
95
96        @Override
97        public void onServiceDisconnected(ComponentName name) {
98            Log.d(TAG, "onServiceDisconnected for " + name);
99            mDMClientService = null;
100        }
101    }
102
103    @Override
104    public void onCreate(Bundle savedInstanceState) {
105        super.onCreate(savedInstanceState);
106        setContentView(R.layout.get_dm_tree_activity);
107    }
108
109    @Override
110    protected void onResume() {
111        super.onResume();
112        // bind to the OMA DM client service
113        Intent intent = new Intent();
114        intent.setClassName("com.android.omadm.service", "com.android.omadm.service.DMClientService");
115        bindService(intent, mDMServiceConnector,
116                Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT);
117    }
118
119    @Override
120    protected void onPause() {
121        // unbind the OMA DM client service
122        unbindService(mDMServiceConnector);
123        super.onPause();
124    }
125}
126