Android Open Source - wifi-auto-forget Auto Forget Delegate






From Project

Back to project page wifi-auto-forget.

License

The source code is released under:

GNU General Public License

If you think the Android project wifi-auto-forget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cebesius.wifiautoforget.util;
/*from  www .  j  av  a2 s.  c  o  m*/
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.cebesius.wifiautoforget.domain.AutoForgetWifi;
import com.cebesius.wifiautoforget.gateway.AutoForgetWifiStorage;

import java.util.List;


/**
 * This delegate implements the AutoForget flow
 */
public class AutoForgetDelegate {

    private static final String TAG = AutoForgetWifi.Behavior.class.getSimpleName();

    private final AutoForgetWifiStorage autoForgetWifiStorage;
    private final WifiManager wifiManager;

    public AutoForgetDelegate(AutoForgetWifiStorage autoForgetWifiStorage, WifiManager wifiManager) {
        this.autoForgetWifiStorage = autoForgetWifiStorage;
        this.wifiManager = wifiManager;
    }

    /**
     * Forgets and purges all SingleAutoForget networks.
     * Forgets all Permanent AutoForget networks.
     */
    public void autoForget() {
        List<AutoForgetWifi> autoForgetWifis = autoForgetWifiStorage.getAllAutoForgetWifis();
        WifiManager.WifiLock wifiLock = wifiManager.createWifiLock(AutoForgetDelegate.class.getCanonicalName());
        wifiLock.acquire();
        for (AutoForgetWifi autoForgetWifi : autoForgetWifis) {
            AutoForgetWifi.Behavior behavior = autoForgetWifi.getBehavior();
            if (behavior == null) {
                continue;
            }
            switch (autoForgetWifi.getBehavior()) {
                case SINGLE:
                    boolean wasForgotten = forget(autoForgetWifi);
                    if (wasForgotten) {
                        purge(autoForgetWifi);
                    }
                    break;
                case PERMANENT:
                    forget(autoForgetWifi);
                    break;
                case NEVER:
                    // no-op
                    break;
            }
        }
        wifiLock.release();
    }

    /**
     * Delete a Network from storage, unless currently connected to it
     * @param autoForgetWifi the network to delete
     *
     */
    private void purge(AutoForgetWifi autoForgetWifi) {
        if (isCurrentlyConnected(autoForgetWifi)) {
            // Do not purge connected wifis
            return;
        }
        autoForgetWifiStorage.delete(autoForgetWifi);
    }

    /**
     * Tell the system WifiManager to forget the Network, unless currently connected to it
     * @param autoForgetWifi
     * @return true if the network was forgotten or false if not
     */
    private boolean forget(AutoForgetWifi autoForgetWifi) {
        if (autoForgetWifi == null) {
            return false;
        }
        if (isCurrentlyConnected(autoForgetWifi)) {
            // Do not forget connected wifis
            return false;
        }
        List<WifiConfiguration> wifiConfigurations = wifiManager.getConfiguredNetworks();
        if (wifiConfigurations == null) {
            // WifiManager can't even tell us what networks it has
            return false;
        }
        for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
            if (autoForgetWifi.represents(wifiConfiguration)) {
                wifiManager.removeNetwork(wifiConfiguration.networkId);
                return wifiManager.saveConfiguration();
            }
        }
        return false; // no match was found
    }

    private boolean isCurrentlyConnected(AutoForgetWifi autoForgetWifi) {
        if (autoForgetWifi == null) {
            return false;
        }
        WifiInfo connectedWifi = wifiManager.getConnectionInfo();
        return autoForgetWifi.represents(connectedWifi);
    }
}




Java Source Code List

antoche.HockeySender.java
com.cebesius.wifiautoforget.Secrets.java
com.cebesius.wifiautoforget.WifiAutoForgetApplication.java
com.cebesius.wifiautoforget.activity.AutoForgetWifisActivity.java
com.cebesius.wifiautoforget.adapter.AutoForgetWifisAdapter.java
com.cebesius.wifiautoforget.dialog.ChangeAutoForgetBehaviorDialog.java
com.cebesius.wifiautoforget.domain.AutoForgetWifi.java
com.cebesius.wifiautoforget.gateway.AddWifiNotificationUsageStorage.java
com.cebesius.wifiautoforget.gateway.AutoForgetWifiStorage.java
com.cebesius.wifiautoforget.gateway.NotificationIds.java
com.cebesius.wifiautoforget.gateway.SharedPreferenceKeys.java
com.cebesius.wifiautoforget.gateway.UserPreferenceStorage.java
com.cebesius.wifiautoforget.model.AutoForgetWifiModel.java
com.cebesius.wifiautoforget.mvp.ActivityView.java
com.cebesius.wifiautoforget.mvp.AddWifiModel.java
com.cebesius.wifiautoforget.mvp.AddWifiPresenter.java
com.cebesius.wifiautoforget.mvp.AddWifiView.java
com.cebesius.wifiautoforget.mvp.AppModel.java
com.cebesius.wifiautoforget.mvp.AppPresenter.java
com.cebesius.wifiautoforget.mvp.AppView.java
com.cebesius.wifiautoforget.mvp.AutoForgetWifisModel.java
com.cebesius.wifiautoforget.mvp.AutoForgetWifisPresenter.java
com.cebesius.wifiautoforget.mvp.AutoForgetWifisView.java
com.cebesius.wifiautoforget.mvp.ConnectivityChangeModel.java
com.cebesius.wifiautoforget.mvp.ConnectivityChangePresenter.java
com.cebesius.wifiautoforget.mvp.ConnectivityChangeView.java
com.cebesius.wifiautoforget.receiver.ConnectivityChangeReceiver.java
com.cebesius.wifiautoforget.service.AddWifiService.java
com.cebesius.wifiautoforget.service.ConnectivityChangeService.java
com.cebesius.wifiautoforget.util.AutoForgetDelegate.java
com.cebesius.wifiautoforget.util.BusPortal.java
com.cebesius.wifiautoforget.widget.AutoForgetWifiItem.java