Search in sources :

Example 11 with WifiManager

use of android.net.wifi.WifiManager in project iosched by google.

the class WiFiUtils method shouldBypassWiFiSetup.

/**
     * Helper method to decide whether to bypass conference WiFi setup.  Return true if
     * WiFi AP is already configured (WiFi adapter enabled) or WiFi configuration is complete
     * as per shared preference.
     */
public static boolean shouldBypassWiFiSetup(final Context context) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // Is WiFi on?
    if (wifiManager.isWifiEnabled()) {
        // Check for existing APs.
        final List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
        final String conferenceSSID = getConferenceWifiConfig().SSID;
        for (WifiConfiguration config : configs) {
            if (conferenceSSID.equalsIgnoreCase(config.SSID))
                return true;
        }
    }
    return WIFI_CONFIG_DONE.equals(getWiFiConfigStatus(context));
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiConfiguration(android.net.wifi.WifiConfiguration)

Example 12 with WifiManager

use of android.net.wifi.WifiManager in project iosched by google.

the class WiFiUtils method isWiFiApConfigured.

public static boolean isWiFiApConfigured(final Context context) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
    if (configs == null)
        return false;
    // Check for existing APs.
    final String conferenceSSID = getConferenceWifiConfig().SSID;
    for (WifiConfiguration config : configs) {
        if (conferenceSSID.equalsIgnoreCase(config.SSID))
            return true;
    }
    return false;
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiConfiguration(android.net.wifi.WifiConfiguration)

Example 13 with WifiManager

use of android.net.wifi.WifiManager in project newsrob by marianokamp.

the class WebPageDownloadTask method doSync.

protected synchronized void doSync(final boolean uploadOnly, final boolean manualSync) {
    PL.log(this, "doSync invoked. (-1) wl=" + wl, null, getApplicationContext());
    final Context ctx = getApplicationContext();
    try {
        PL.log(this, "doSync invoked. (0)", null, getApplicationContext());
        // (WifiManager)
        WifiManager wifiManager = null;
        if (false) {
            WifiLock wiFiLock = wifiManager.createWifiLock("NewsRobSync");
            wiFiLock.acquire();
        }
        PL.log(this, "doSync invoked. (1)", null, getApplicationContext());
        U.setLowPrio();
        PL.log(this, "doSync invoked. (2)", null, getApplicationContext());
        final EntryManager entryManager = getEntryManager();
        final EntriesRetriever grf = entryManager.getEntriesRetriever();
        final IStorageAdapter fileContextAdapter = entryManager.getStorageAdapter();
        PL.log(this, "doSync invoked. (3)", null, getApplicationContext());
        if (entryManager.isModelCurrentlyUpdated()) {
            PL.log(this, "doSync invoked. (3.4)", null, getApplicationContext());
            return;
        }
        PL.log(this, "doSync invoked. (3.5)", null, getApplicationContext());
        entryManager.lockModel("SSer.doSync");
        PL.log(this, "doSync invoked. (4)", null, getApplicationContext());
        PL.log("SynchronizationService. Used settings: " + SettingsRenderer.renderSettings(entryManager, new StringBuilder("\n")), SynchronizationService.this);
        Throwable caughtThrowable = null;
        PL.log("SynchronizationService - start", SynchronizationService.this);
        PL.log("Battery level=" + U.getBatteryChargedPercent(ctx) + "%.", ctx);
        PL.log("Last successful login: " + entryManager.getLastSuccessfulLogin(), SynchronizationService.this);
        Timing t = new Timing("Synchronization Runnable", this);
        long started = System.currentTimeMillis();
        Log.i(TAG, "Synchronization started at " + new Date().toString() + ". started=" + started);
        final SyncJobStatus syncJobStatus = new SyncJobStatus();
        // last used
        long lastUsed = entryManager.getLastUsed();
        final DownloadContext downloadContext = new DownloadContext();
        PL.log(this, "doSync invoked. (5)", null, getApplicationContext());
        ModelUpdateResult result = null;
        try {
            PL.log("Run Mark - in Try", SynchronizationService.this);
            if (!uploadOnly) {
                try {
                    if (!Feed.restoreFeedsIfNeccesary(this))
                        Feed.saveFeedSettings(this);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            entryManager.fireModelUpdateStarted("Synchronization", uploadOnly, manualSync);
            List<Job> jobList = new LinkedList<Job>();
            Job deleteReadArticlesJob = new DeleteArticlesJob(SynchronizationService.this, entryManager, syncJobStatus);
            Job reduceToCapacityJob = new ReduceToCapacityJob(SynchronizationService.this, entryManager, syncJobStatus);
            if (!uploadOnly) {
                if (entryManager.shouldReadItemsBeDeleted())
                    jobList.add(deleteReadArticlesJob);
                jobList.add(reduceToCapacityJob);
            }
            if (false)
                jobList.add(new Job("Submitting annotated articles", entryManager) {

                    @Override
                    public void run() throws Exception {
                        if (entryManager.syncCurrentlyEnabled(manualSync))
                            entryManager.getEntriesRetriever().submitNotes(this);
                    }
                });
            jobList.add(new SyncChangedArticlesStatusJob(SynchronizationService.this, entryManager, syncJobStatus, manualSync));
            if (!uploadOnly && entryManager.shouldReadItemsBeDeleted())
                jobList.add(deleteReadArticlesJob);
            if (!uploadOnly) {
                jobList.add(new Job("Unsubscribing from feeds", entryManager) {

                    @Override
                    public void run() throws Exception {
                        if (!entryManager.syncCurrentlyEnabled(manualSync))
                            return;
                        Cursor c = entryManager.getFeeds2UnsubscribeCursor();
                        try {
                            while (c.moveToNext()) {
                                String feedAtomId = c.getString(1);
                                PL.log("Unsubscribing: " + feedAtomId, SynchronizationService.this);
                                entryManager.getEntriesRetriever().unsubscribeFeed(feedAtomId);
                            }
                        } finally {
                            c.close();
                        }
                    }
                });
            }
            if (!uploadOnly)
                jobList.add(new FetchUnreadArticlesJob(SynchronizationService.this, entryManager, syncJobStatus, manualSync));
            if (!uploadOnly)
                jobList.add(new Job("Daily update of subscriptions (feed titles)", entryManager) {

                    @Override
                    public void run() throws IOException, ParserConfigurationException, SAXException, GRTokenExpiredException {
                        if (entryManager.syncCurrentlyEnabled(manualSync)) {
                            grf.updateSubscriptionList(entryManager, this);
                            entryManager.fireModelUpdated();
                        }
                    }
                });
            if (!uploadOnly)
                jobList.add(reduceToCapacityJob);
            if (!uploadOnly && entryManager.shouldReadItemsBeDeleted())
                jobList.add(deleteReadArticlesJob);
            PL.log(this, "doSync invoked. (6)", null, getApplicationContext());
            if (!uploadOnly) {
                // make sure that a manual sync moves the automatic sync
                // forward,
                // i.e. when pushing "Refresh" in the middle of a 24h sync,
                // reset timer to 0, so that it will be another 24h from
                // this
                // point on
                entryManager.getScheduler().resetBackgroundSchedule();
                final SyncJobStatus sjStatus = new SyncJobStatus();
                jobList.add(new SyncJob(ctx, entryManager, sjStatus, "Downloading articles") {

                    private Collection<Long> entries2Download;

                    @Override
                    public int doRun() {
                        if (entryManager.getSharedPreferences().getString(EntryManager.SETTINGS_STORAGE_ASSET_DOWNLOAD, EntryManager.DOWNLOAD_YES).equals(EntryManager.DOWNLOAD_NO)) {
                            Log.d(TAG, "Downloading of assets is disabled in the settings. Therefore skipping downloading webpages.");
                            return actual;
                        }
                        Timing tSql = new Timing("SQL Query findAllToDownload", SynchronizationService.this);
                        entries2Download = entryManager.findAllArticleIds2Download();
                        target = entries2Download.size();
                        tSql.stop();
                        Timing tOutter = new Timing("Downloading all " + entries2Download.size() + " pages or well, the ones that were downloaded", SynchronizationService.this);
                        // shouldDownloadArticlesInParallel = true;
                        final int numberOfThreads = shouldDownloadArticlesInParallel && !U.isScreenOn(ctx) ? 3 : 1;
                        PL.log("Instantiating Download Articles ScheduledExecutorService for " + numberOfThreads + " threads.", ctx);
                        final ScheduledExecutorService pool = Executors.newScheduledThreadPool(numberOfThreads);
                        int count = 0;
                        try {
                            actual = 1;
                            entryManager.fireStatusUpdated();
                            for (Long articleId : entries2Download) {
                                // get the current data
                                // LATER use a real cursor and somehow find
                                // out
                                // when
                                // data became stale
                                Entry entry = entryManager.findArticleById(articleId);
                                if (entry == null)
                                    continue;
                                if (!entryManager.downloadContentCurrentlyEnabled(manualSync))
                                    return actual;
                                if (!fileContextAdapter.canWrite()) {
                                    Log.d(TAG, "File context adapter (" + fileContextAdapter.getClass().getName() + ") cannot be written to at the moment. Mounted? Read Only? Not downloading web pages.");
                                    return actual;
                                }
                                if (isCancelled())
                                    break;
                                if (entry.getReadState() == ReadState.READ && !entry.isStarred())
                                    continue;
                                int resolvedDownloadPref = entry.getResolvedDownloadPref(entryManager);
                                if (resolvedDownloadPref == Feed.DOWNLOAD_HEADERS_ONLY) {
                                    continue;
                                }
                                // check against the db, because in the
                                // meantime
                                // the
                                // read status might have changed
                                boolean downloadTheWholePage = (resolvedDownloadPref == Feed.DOWNLOAD_PREF_FEED_AND_MOBILE_WEBPAGE || resolvedDownloadPref == Feed.DOWNLOAD_PREF_FEED_AND_WEBPAGE);
                                String summary = entry.getContent() != null ? entry.getContent() : UIHelper.linkize(entry.getAlternateHRef(), entry.getTitle());
                                WebPageDownloadTask task = new WebPageDownloadTask(entryManager, fileContextAdapter, this, entry, summary, downloadTheWholePage, manualSync, downloadContext);
                                if (true)
                                    // pool.submit(task);
                                    pool.schedule(task, count++ * 500, TimeUnit.MILLISECONDS);
                                else
                                    try {
                                        task.call();
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                            }
                        } finally {
                            PL.log(this, "doSync invoked. (6.1 Pool Shutdown 1)", null, getApplicationContext());
                            pool.shutdown();
                            PL.log(this, "doSync invoked. (6.2 Pool Shutdown 2)", null, getApplicationContext());
                            try {
                                while (true) {
                                    // wait and check if the pool is done.
                                    boolean terminated = pool.awaitTermination(2, TimeUnit.SECONDS);
                                    // done?
                                    if (terminated)
                                        break;
                                    boolean terminate = false;
                                    if (!entryManager.downloadContentCurrentlyEnabled(manualSync))
                                        terminate = true;
                                    if (!terminate && !fileContextAdapter.canWrite()) {
                                        Log.d(TAG, "File context adapter (" + fileContextAdapter.getClass().getName() + ") cannot be written to at the moment. Mounted? Read Only? Not downloading web pages.");
                                        terminate = true;
                                    }
                                    if (!terminate && isCancelled())
                                        terminate = true;
                                    if (terminate) {
                                        PL.log("Terminating downloadpagetask pool", ctx);
                                        pool.shutdownNow();
                                        break;
                                    }
                                // all good so far go back to the
                                // beginning
                                // and check those
                                }
                            } catch (InterruptedException e) {
                                // Ignore
                                e.printStackTrace();
                                PL.log(this, "Interrupted Exception", e, ctx);
                            } finally {
                                PL.log(this, "doSync invoked. (6.3 Pool ShutdownNow 1)", null, getApplicationContext());
                                pool.shutdownNow();
                                PL.log(this, "doSync invoked. (6.4 Pool ShutdownNow 2)", null, getApplicationContext());
                            }
                        }
                        tOutter.stop();
                        return actual;
                    }
                });
                jobList.add(new Job("Vacuuming database ... ", entryManager) {

                    @Override
                    public void run() throws Throwable {
                        entryManager.vacuumDb();
                    }
                });
            }
            PL.log("Run Mark - Jobs added", this);
            PL.log(this, "doSync invoked. (7)", null, getApplicationContext());
            entryManager.runJobs(jobList);
            PL.log(this, "doSync invoked. (7.1 After Run Jobs)", null, getApplicationContext());
            Log.d(TAG, "NoOfEntriesUpdated=" + syncJobStatus.noOfEntriesUpdated);
            Log.d(TAG, "NoOfEntriesFetched=" + syncJobStatus.noOfEntriesFetched);
            PL.log("Run Mark - Mission accomplished. -> complete ", this);
            result = new SynchronizeModelSucceeded(syncJobStatus.noOfEntriesUpdated);
        } catch (Throwable throwable) {
            result = new SynchronizeModelFailed(throwable);
            Log.d(TAG, "Problem during synchronization.", throwable);
            PL.log(this, "Problem during synchronization", throwable, ctx);
        } finally {
            PL.log("Run Mark - In Finally", this);
            entryManager.unlockModel("SSer.doSync");
            entryManager.clearCancelState();
            entryManager.fireModelUpdateFinished(result);
            entryManager.fireStatusUpdated();
            Log.i(TAG, "Synchronization finished at " + new Date().toString() + ". started=" + started);
            t.stop();
            PL.log(this, "doSync invoked. (7.2)", null, getApplicationContext());
            if (!uploadOnly)
                entryManager.setLastSync(caughtThrowable == null);
            int noOfNewArticles = entryManager.getNoOfNewArticlesSinceLastUsed(lastUsed);
            entryManager.getNewsRobNotificationManager().notifyNewArticles(entryManager, lastUsed, noOfNewArticles);
            PL.log("Run Mark - End of Finally", this);
            PL.log("Battery level=" + U.getBatteryChargedPercent(ctx) + "%.", ctx);
            resetLastStarted();
            PL.log(this, "doSync invoked. (8)", null, getApplicationContext());
        }
    } finally {
        PL.log(this, "doSync invoked. (9)", null, getApplicationContext());
        releaseWakeLock(ctx);
        PL.log(this, "doSync invoked. (9.1)", null, getApplicationContext());
    // wiFiLock.release(); // TODO
    }
    PL.log(this, "doSync invoked. (10)", null, getApplicationContext());
    PL.log("SynchronizationService. Used settings: " + SettingsRenderer.renderSettings(entryManager, new StringBuilder("\n")), SynchronizationService.this);
}
Also used : WifiManager(android.net.wifi.WifiManager) SyncJobStatus(com.newsrob.EntryManager.SyncJobStatus) Cursor(android.database.Cursor) Job(com.newsrob.jobs.Job) Context(android.content.Context) DownloadContext(com.newsrob.download.DownloadContext) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SynchronizeModelFailed(com.newsrob.jobs.SynchronizeModelFailed) ModelUpdateResult(com.newsrob.jobs.ModelUpdateResult) SynchronizeModelSucceeded(com.newsrob.jobs.SynchronizeModelSucceeded) WifiLock(android.net.wifi.WifiManager.WifiLock) Date(java.util.Date) DownloadTimedOutException(com.newsrob.download.DownloadTimedOutException) NoHttpResponseException(org.apache.http.NoHttpResponseException) ParseException(java.text.ParseException) FileNotFoundException(java.io.FileNotFoundException) DownloadException(com.newsrob.download.DownloadException) SAXException(org.xml.sax.SAXException) DownloadCancelledException(com.newsrob.download.DownloadCancelledException) ClientProtocolException(org.apache.http.client.ClientProtocolException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LinkedList(java.util.LinkedList) DownloadContext(com.newsrob.download.DownloadContext) IStorageAdapter(com.newsrob.storage.IStorageAdapter) Timing(com.newsrob.util.Timing)

Example 14 with WifiManager

use of android.net.wifi.WifiManager in project android-common by litesuits.

the class AndroidUtil method getWifiMacAddress.

/**
     * 获取 Wifi MAC 地址
     * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     */
public static String getWifiMacAddress(Context context) {
    //wifi mac地址
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    String mac = info.getMacAddress();
    if (Log.isPrint) {
        Log.i(TAG, "WIFI MAC:" + mac);
    }
    return mac;
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiInfo(android.net.wifi.WifiInfo)

Example 15 with WifiManager

use of android.net.wifi.WifiManager in project robolectric by robolectric.

the class ShadowWifiInfoTest method shouldReturnRssi.

@Test
public void shouldReturnRssi() {
    WifiManager wifiManager = (WifiManager) application.getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    // WifiInfo.INVALID_RSSI
    assertThat(wifiInfo.getRssi()).isEqualTo(-127);
    shadowOf(wifiInfo).setRssi(10);
    wifiManager = (WifiManager) application.getSystemService(WIFI_SERVICE);
    wifiInfo = wifiManager.getConnectionInfo();
    assertThat(wifiInfo.getRssi()).isEqualTo(10);
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiInfo(android.net.wifi.WifiInfo) Test(org.junit.Test)

Aggregations

WifiManager (android.net.wifi.WifiManager)169 WifiInfo (android.net.wifi.WifiInfo)53 WifiConfiguration (android.net.wifi.WifiConfiguration)42 IOException (java.io.IOException)24 IntentFilter (android.content.IntentFilter)13 WifiEnterpriseConfig (android.net.wifi.WifiEnterpriseConfig)10 Context (android.content.Context)9 ConnectivityManager (android.net.ConnectivityManager)8 NetworkInfo (android.net.NetworkInfo)8 Test (org.junit.Test)8 Intent (android.content.Intent)7 File (java.io.File)6 SAXException (org.xml.sax.SAXException)6 SharedPreferences (android.content.SharedPreferences)5 PowerManager (android.os.PowerManager)5 TelephonyManager (android.telephony.TelephonyManager)5 X509Certificate (java.security.cert.X509Certificate)5 BufferedReader (java.io.BufferedReader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileReader (java.io.FileReader)4