Search in sources :

Example 1 with WifiP2pManager

use of android.net.wifi.p2p.WifiP2pManager in project physical-web by google.

the class FileBroadcastService method changeWifiName.

private void changeWifiName() {
    String deviceName = "PW-" + mTitle + "-" + mPort;
    if (deviceName.length() > MAX_DEVICE_NAME_LENGTH) {
        deviceName = DEFAULT_DEVICE_NAME + mPort;
    }
    try {
        WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
        WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), null);
        Class[] paramTypes = new Class[3];
        paramTypes[0] = WifiP2pManager.Channel.class;
        paramTypes[1] = String.class;
        paramTypes[2] = WifiP2pManager.ActionListener.class;
        Method setDeviceName = manager.getClass().getMethod("setDeviceName", paramTypes);
        setDeviceName.setAccessible(true);
        Object[] arglist = new Object[3];
        arglist[0] = channel;
        arglist[1] = deviceName;
        arglist[2] = new WifiP2pManager.ActionListener() {

            @Override
            public void onSuccess() {
                Log.d(TAG, "setDeviceName succeeded");
            }

            @Override
            public void onFailure(int reason) {
                Log.d(TAG, "setDeviceName failed");
            }
        };
        setDeviceName.invoke(manager, arglist);
    } catch (NoSuchMethodException e) {
        Log.d(TAG, e.getMessage());
    } catch (IllegalAccessException e) {
        Log.d(TAG, e.getMessage());
    } catch (IllegalArgumentException e) {
        Log.d(TAG, e.getMessage());
    } catch (InvocationTargetException e) {
        Log.d(TAG, e.getMessage());
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) WifiP2pManager(android.net.wifi.p2p.WifiP2pManager)

Example 2 with WifiP2pManager

use of android.net.wifi.p2p.WifiP2pManager in project physical-web by google.

the class FileBroadcastService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (mFileBroadcastServer != null) {
        mFileBroadcastServer.stop();
    }
    mType = intent.getStringExtra(MIME_TYPE_KEY);
    Log.d(TAG, mType);
    mUri = Uri.parse(intent.getStringExtra(FILE_KEY));
    Log.d(TAG, mUri.toString());
    mTitle = intent.getStringExtra(TITLE_KEY);
    mTitle = mTitle == null ? "Share" : mTitle;
    mPort = Utils.getWifiDirectPort(this);
    try {
        mFile = Utils.getBytes(getContentResolver().openInputStream(mUri));
    } catch (FileNotFoundException e) {
        Log.d(TAG, e.getMessage());
        stopSelf();
        return START_STICKY;
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
        stopSelf();
        return START_STICKY;
    }
    mNotificationManager = NotificationManagerCompat.from(this);
    mFileBroadcastServer = new FileBroadcastServer(mPort, mType, mFile);
    try {
        mFileBroadcastServer.start();
        Utils.createBroadcastNotification(this, stopServiceReceiver, BROADCASTING_NOTIFICATION_ID, getString(R.string.wifi_direct_notification_title), Integer.toString(mPort), "myFilter2");
    } catch (IOException e) {
        Log.d(TAG, e.getMessage());
        stopSelf();
        return START_STICKY;
    }
    sendBroadcast(new Intent("server"));
    WifiP2pManager mManager = (WifiP2pManager) this.getSystemService(Context.WIFI_P2P_SERVICE);
    WifiP2pManager.Channel mChannel = mManager.initialize(this, this.getMainLooper(), null);
    changeWifiName();
    mManager.discoverPeers(mChannel, new WifiP2pManager.ActionListener() {

        @Override
        public void onSuccess() {
            Log.d(TAG, "discovering");
        }

        @Override
        public void onFailure(int reasonCode) {
            Log.d(TAG, "discovery failed " + reasonCode);
        }
    });
    Toast.makeText(this, R.string.wifi_direct_broadcasting_confirmation, Toast.LENGTH_SHORT).show();
    return START_STICKY;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Intent(android.content.Intent) IOException(java.io.IOException) WifiP2pManager(android.net.wifi.p2p.WifiP2pManager)

Example 3 with WifiP2pManager

use of android.net.wifi.p2p.WifiP2pManager in project robolectric by robolectric.

the class ShadowContextImpl method getSystemService.

@Implementation
public Object getSystemService(String name) {
    if (name.equals(Context.LAYOUT_INFLATER_SERVICE)) {
        return new RoboLayoutInflater(RuntimeEnvironment.application);
    }
    Object service = systemServices.get(name);
    if (service == null) {
        String serviceClassName = SYSTEM_SERVICE_MAP.get(name);
        if (serviceClassName == null) {
            System.err.println("WARNING: unknown service " + name);
            return null;
        }
        try {
            Class<?> clazz = Class.forName(serviceClassName);
            if (serviceClassName.equals("android.app.admin.DevicePolicyManager")) {
                if (getApiLevel() >= N) {
                    service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(IDevicePolicyManager.class, null), ClassParameter.from(boolean.class, false));
                } else {
                    service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(Handler.class, null));
                }
            } else if (serviceClassName.equals("android.app.SearchManager") || serviceClassName.equals("android.app.ActivityManager") || serviceClassName.equals("android.app.WallpaperManager")) {
                service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(Handler.class, null));
            } else if (serviceClassName.equals("android.os.storage.StorageManager")) {
                service = ReflectionHelpers.callConstructor(clazz);
            } else if (serviceClassName.equals("android.nfc.NfcManager") || serviceClassName.equals("android.telecom.TelecomManager")) {
                service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application));
            } else if (serviceClassName.equals("android.hardware.display.DisplayManager") || serviceClassName.equals("android.telephony.SubscriptionManager")) {
                service = ReflectionHelpers.callConstructor(clazz, ClassParameter.from(Context.class, RuntimeEnvironment.application));
            } else if (serviceClassName.equals("android.view.accessibility.AccessibilityManager")) {
                service = AccessibilityManager.getInstance(realObject);
            } else if (getApiLevel() >= JELLY_BEAN_MR1 && serviceClassName.equals("android.view.WindowManagerImpl")) {
                Class<?> windowMgrImplClass = Class.forName("android.view.WindowManagerImpl");
                if (getApiLevel() >= N) {
                    service = ReflectionHelpers.callConstructor(windowMgrImplClass, ClassParameter.from(Context.class, realObject));
                } else {
                    Display display = newInstanceOf(Display.class);
                    service = ReflectionHelpers.callConstructor(windowMgrImplClass, ClassParameter.from(Display.class, display));
                }
            } else if (serviceClassName.equals("android.accounts.AccountManager")) {
                service = ReflectionHelpers.callConstructor(Class.forName("android.accounts.AccountManager"), ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(IAccountManager.class, null));
            } else if (serviceClassName.equals("android.net.wifi.p2p.WifiP2pManager")) {
                service = new WifiP2pManager(ReflectionHelpers.createNullProxy(IWifiP2pManager.class));
            } else if (getApiLevel() >= KITKAT && serviceClassName.equals("android.print.PrintManager")) {
                service = ReflectionHelpers.callConstructor(Class.forName("android.print.PrintManager"), ClassParameter.from(Context.class, RuntimeEnvironment.application), ClassParameter.from(android.print.IPrintManager.class, null), ClassParameter.from(int.class, -1), ClassParameter.from(int.class, -1));
            } else {
                service = newInstanceOf(clazz);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        systemServices.put(name, service);
    }
    return service;
}
Also used : Context(android.content.Context) IAccountManager(android.accounts.IAccountManager) Handler(android.os.Handler) RealObject(org.robolectric.annotation.RealObject) WifiP2pManager(android.net.wifi.p2p.WifiP2pManager) IWifiP2pManager(android.net.wifi.p2p.IWifiP2pManager) Display(android.view.Display) Implementation(org.robolectric.annotation.Implementation)

Aggregations

WifiP2pManager (android.net.wifi.p2p.WifiP2pManager)3 IAccountManager (android.accounts.IAccountManager)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IWifiP2pManager (android.net.wifi.p2p.IWifiP2pManager)1 Handler (android.os.Handler)1 Display (android.view.Display)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Implementation (org.robolectric.annotation.Implementation)1 RealObject (org.robolectric.annotation.RealObject)1