Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f8a78d0a authored by Mike Kasick's avatar Mike Kasick
Browse files

Port WimaxHelper and (the toggle) WimaxButton from CM7.

Both WimaxHelper and WimaxButton are updated to support Samsung's
(crespo4g, epicmtd) Wimax4GManager proprietary class, whose API has been
somewhat-significantly changed in ICS.  Support for HTC's (supersonic,
speedy) WimaxController is retained in WimaxHelper, although untested.

Note that WimaxManagerConstants was previously misdocumented, confabulating
the lookup key EXTRA_4G_STATE ("4g_state") with EXTRA_WIMAX_STATUS
("wimax_status"), which no longer exists.  This is also fixed.

Change-Id: If6fc4a0a290ad9689a40ee06f9f6a2f3092cfaf9
parent 11638519
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -61,6 +61,8 @@ import android.net.wifi.IWifiManager;
import android.net.wifi.WifiManager;
import android.net.wifi.p2p.IWifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wimax.WimaxHelper;
import android.net.wimax.WimaxManagerConstants;
import android.nfc.NfcManager;
import android.os.Binder;
import android.os.Bundle;
@@ -452,6 +454,11 @@ class ContextImpl extends Context {
                public Object getService(ContextImpl ctx) {
                    return WindowManagerImpl.getDefault(ctx.mPackageInfo.mCompatibilityInfo);
                }});

        registerService(WimaxManagerConstants.WIMAX_SERVICE, new ServiceFetcher() {
                public Object createService(ContextImpl ctx) {
                    return WimaxHelper.createWimaxService(ctx, ctx.mMainThread.getHandler());
                }});
    }

    static ContextImpl getImpl(Context context) {
+188 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}
+16 −2
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ public class WimaxManagerConstants
     * The lookup key for an int that indicates whether Wimax is enabled,
     * disabled, enabling, disabling, or unknown.
     */
    public static final String EXTRA_WIMAX_STATUS = "wimax_status";
    public static final String EXTRA_4G_STATE = "4g_state";

    /**
     * Broadcast intent action indicating that Wimax state has been changed
@@ -48,7 +48,6 @@ public class WimaxManagerConstants
     * initializing, initialized, unknown and ready.
     */
    public static final String EXTRA_WIMAX_STATE = "WimaxState";
    public static final String EXTRA_4G_STATE = "4g_state";
    public static final String EXTRA_WIMAX_STATE_INT = "WimaxStateInt";
    /**
     * The lookup key for an int that indicates whether state of Wimax
@@ -66,11 +65,21 @@ public class WimaxManagerConstants
     */
    public static final int NET_4G_STATE_DISABLED = 1;

    /**
     * Indicatates Wimax is disabling.
     */
    public static final int NET_4G_STATE_DISABLING = 0;

    /**
     * Indicatates Wimax is enabled.
     */
    public static final int NET_4G_STATE_ENABLED = 3;

    /**
     * Indicatates Wimax is enabling.
     */
    public static final int NET_4G_STATE_ENABLING = 2;

    /**
     * Indicatates Wimax status is known.
     */
@@ -101,4 +110,9 @@ public class WimaxManagerConstants
     */
    public static final int WIMAX_STATE_DISCONNECTED = 9;

    /**
     * Constants for HTC/SQN WiMAX implementation
     */
    public static final String WIMAX_ENABLED_CHANGED_ACTION = "com.htc.net.wimax.WIMAX_ENABLED_CHANGED";
    public static final String CURRENT_WIMAX_ENABLED_STATE  = "curWimaxEnabledState";
}
+6 −0
Original line number Diff line number Diff line
@@ -3486,6 +3486,12 @@ public final class Settings {
        public static final String WIFI_MOBILE_DATA_TRANSITION_WAKELOCK_TIMEOUT_MS =
            "wifi_mobile_data_transition_wakelock_timeout_ms";

        /**
         * Whether the Wimax should be on.  Only the WiMAX service should touch this.
         * @hide
         */
        public static final String WIMAX_ON = "wimax_on";

        /**
         * Whether background data usage is allowed by the user. See
         * ConnectivityManager for more info.
+1 −1
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ public abstract class PowerButton {
        BUTTONS.put(BUTTON_MEDIA_PLAY_PAUSE, MediaPlayPauseButton.class);
        BUTTONS.put(BUTTON_MEDIA_PREVIOUS, MediaPreviousButton.class);
        BUTTONS.put(BUTTON_MEDIA_NEXT, MediaNextButton.class);
        /* BUTTONS.put(BUTTON_WIMAX, WimaxButton.class); */
        BUTTONS.put(BUTTON_WIMAX, WimaxButton.class);
    }
    // this is a list of our currently loaded buttons
    private static final HashMap<String, PowerButton> BUTTONS_LOADED = new HashMap<String, PowerButton>();
Loading