summaryrefslogtreecommitdiffstats
path: root/core/java/android/net/wimax/WimaxHelper.java
blob: f6c7a409b5fb5d6d02f59a9299cf0500fa918c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (C) 2011 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.wimax;

import dalvik.system.DexClassLoader;

import android.content.Context;
import android.content.ContextWrapper;
import android.os.Handler;
import android.os.IBinder;
import android.os.ServiceManager;
import android.util.Log;
import android.provider.Settings;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * {@hide}
 */
public class WimaxHelper {

    private static final String TAG = "WimaxHelper";

    private static final String WIMAX_CONTROLLER_CLASSNAME = "com.htc.net.wimax.WimaxController";
    private static final String WIMAX_MANAGER_CLASSNAME    = "android.net.fourG.wimax.Wimax4GManager";

    private static DexClassLoader sWimaxClassLoader;
    private static String sWimaxManagerClassname, sIsWimaxEnabledMethodname,
                          sSetWimaxEnabledMethodname, sGetWimaxStateMethodname;

    public static boolean isWimaxSupported(Context context) {
        return context.getResources().getBoolean(
                com.android.internal.R.bool.config_wimaxEnabled);
    }

    public static DexClassLoader getWimaxClassLoader(Context context) {
        if (isWimaxSupported(context)) {
            if (sWimaxClassLoader == null) {
                sWimaxManagerClassname = context.getResources().getString(
                    com.android.internal.R.string.config_wimaxManagerClassname);

                // WimaxController::getWimaxState == Wimax4GManager::get4GState.
                // However, Wimax4GManager also implements a different getWimaxState
                // method, which returns a WimaxState object describing the connection
                // state, not the enabled state.  Other methods are similarly renamed.
                if (sWimaxManagerClassname.equals(WIMAX_CONTROLLER_CLASSNAME)) {
                    sIsWimaxEnabledMethodname  = "isWimaxEnabled";
                    sSetWimaxEnabledMethodname = "setWimaxEnabled";
                    sGetWimaxStateMethodname   = "getWimaxState";
                } else if (sWimaxManagerClassname.equals(WIMAX_MANAGER_CLASSNAME)) {
                    sIsWimaxEnabledMethodname  = "is4GEnabled";
                    sSetWimaxEnabledMethodname = "set4GEnabled";
                    sGetWimaxStateMethodname   = "get4GState";
                }

                String wimaxJarLocation = context.getResources().getString(
                        com.android.internal.R.string.config_wimaxServiceJarLocation);
                String wimaxLibLocation = context.getResources().getString(
                        com.android.internal.R.string.config_wimaxNativeLibLocation);
                sWimaxClassLoader =  new DexClassLoader(wimaxJarLocation,
                        new ContextWrapper(context).getCacheDir().getAbsolutePath(),
                        wimaxLibLocation,ClassLoader.getSystemClassLoader());
            }
            return sWimaxClassLoader;
        }
        return null;
    }

    public static Object createWimaxService(Context context, Handler handler) {
        Object controller = null;

        try {
            DexClassLoader wimaxClassLoader = getWimaxClassLoader(context);
            if (sWimaxManagerClassname.equals(WIMAX_CONTROLLER_CLASSNAME)) {
                // Load supersonic's and speedy's WimaxController.
                IBinder b = ServiceManager.getService(WimaxManagerConstants.WIMAX_SERVICE);
                if (b != null) {
                    Class<?> klass = wimaxClassLoader.loadClass("com.htc.net.wimax.IWimaxController$Stub");
                    if (klass != null) {
                        Method asInterface = klass.getMethod("asInterface", IBinder.class);
                        Object wc = asInterface.invoke(null, b);
                        if (wc != null) {
                            klass = wimaxClassLoader.loadClass(WIMAX_CONTROLLER_CLASSNAME);
                            if (klass != null) {
                                Constructor<?> ctor = klass.getDeclaredConstructors()[1];
                                controller = ctor.newInstance(wc, handler);
                            }
                        }
                    }
                }
            } else if (sWimaxManagerClassname.equals(WIMAX_MANAGER_CLASSNAME)) {
                // Load crespo4g's (and epicmtd's) Wimax4GManager.
                // Note that crespo4g's implementation grabs WIMAX_SERVICE internally, so
                // it doesn't need to be passed in.  Other implementations (may) require
                // WIMAX_SERVICE to be grabbed externally, so check Wimax4GManager::<init>.
                Class<?> klass = wimaxClassLoader.loadClass(WIMAX_MANAGER_CLASSNAME);
                if (klass != null) {
                    Constructor<?> ctor = klass.getDeclaredConstructors()[0];
                    controller = ctor.newInstance();
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "Unable to create WimaxController instance", e);
        }

        return controller;
    }

    public static boolean isWimaxEnabled(Context context) {
        boolean ret = false;
        try {
            Object wimaxService = context.getSystemService(WimaxManagerConstants.WIMAX_SERVICE);
            Method m = wimaxService.getClass().getMethod(sIsWimaxEnabledMethodname);
            ret = (Boolean) m.invoke(wimaxService);
        } catch (Exception e) {
            Log.e(TAG, "Unable to get WiMAX enabled state!", e);
        }
        return ret;
    }

    public static boolean setWimaxEnabled(Context context, boolean enabled) {
        boolean ret = false;
        try {
            Object wimaxService = context.getSystemService(WimaxManagerConstants.WIMAX_SERVICE);
            Method m = wimaxService.getClass().getMethod(sSetWimaxEnabledMethodname, boolean.class);
            ret = (Boolean) m.invoke(wimaxService, enabled);
            if (ret)
                Settings.Secure.putInt(context.getContentResolver(),
                        Settings.Secure.WIMAX_ON, (Boolean) enabled ? 1 : 0);
        } catch (Exception e) {
            Log.e(TAG, "Unable to set WiMAX state!", e);
        }
        return ret;
    }

    public static int getWimaxState(Context context) {
        int ret = 0;
        try {
            Object wimaxService = context.getSystemService(WimaxManagerConstants.WIMAX_SERVICE);
            Method m = wimaxService.getClass().getMethod(sGetWimaxStateMethodname);
            ret = (Integer) m.invoke(wimaxService);
        } catch (Exception e) {
            Log.e(TAG, "Unable to get WiMAX state!", e);
        }
        return ret;
    }

    public static boolean wimaxRescan(Context context) {
        boolean ret = false;
        try {
            Object wimaxService = context.getSystemService(WimaxManagerConstants.WIMAX_SERVICE);
            Method wimaxRescan = wimaxService.getClass().getMethod("wimaxRescan");
            if (wimaxRescan != null) {
                wimaxRescan.invoke(wimaxService);
                ret = true;
            }
        } catch (Exception e) {
            Log.e(TAG, "Unable to perform WiMAX rescan!", e);
        }
        return ret;
    }

    private static Object getWimaxInfo(Context context) {
        Object wimaxInfo = null;
        try {
            Object wimaxService = context.getSystemService(WimaxManagerConstants.WIMAX_SERVICE);
            Method getConnectionInfo = wimaxService.getClass().getMethod("getConnectionInfo");
            wimaxInfo = getConnectionInfo.invoke(wimaxService);
        } catch (Exception e) {
            Log.e(TAG, "Unable to get a WimaxInfo object!", e);
        }
        return wimaxInfo;
    }
}