<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * 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 com.pyamsoft.powermanager.dagger.observer.state;

import android.content.Context;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;
import com.pyamsoft.powermanager.PowerManagerPreferences;
import java.util.List;
import java.util.concurrent.TimeUnit;
import timber.log.Timber;

class WearStateObserver extends StateObserver {

  @NonNull private final GoogleApiClient googleApiClient;
  @NonNull private final PowerManagerPreferences preferences;

  WearStateObserver(@NonNull Context context, @NonNull PowerManagerPreferences preferences) {
    super(context);
    this.preferences = preferences;
    googleApiClient =
        new GoogleApiClient.Builder(context.getApplicationContext()).addApiIfAvailable(Wearable.API)
            .build();
  }

  @WorkerThread @CheckResult private boolean isWearableNodeConnected() {
    final long waitTime = preferences.getWearableDelay();
    Timber.d("Wait for connection for %d seconds", waitTime);
    final NodeApi.GetConnectedNodesResult nodesResult =
        Wearable.NodeApi.getConnectedNodes(googleApiClient).await(waitTime, TimeUnit.SECONDS);
    Node wearableNode = null;
    final List<Node> nodes = nodesResult.getNodes();
    Timber.d("Search node list of size : %d", nodes.size());
    for (final Node node : nodes) {
      if (node.isNearby()) {
        Timber.d("Wearable node: %s %s", node.getDisplayName(), node.getId());
        wearableNode = node;
        break;
      }
    }

    final boolean result;
    if (wearableNode == null) {
      Timber.w("No wearable node was found");
      result = true;
    } else {
      Timber.d("Found a wearable node");
      result = false;
    }

    disconnectGoogleApiClient();
    return result;
  }

  /**
   * Return true if stream should continue, false otherwise
   */
  @WorkerThread @Override public boolean is() {
    Timber.d("Check if wearable is connected");
    final ConnectionResult connectionResult = googleApiClient.blockingConnect(4, TimeUnit.SECONDS);
    boolean result;
    if (connectionResult.isSuccess()) {
      Timber.d("Connect Google APIs");
      result = isWearableNodeConnected();
    } else {
      Timber.e("Could not connect to Google APIs");<commit_msg>Use preference time for connections<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * 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 com.pyamsoft.powermanager.dagger.observer.state;

import android.content.Context;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;
import com.pyamsoft.powermanager.PowerManagerPreferences;
import java.util.List;
import java.util.concurrent.TimeUnit;
import timber.log.Timber;

class WearStateObserver extends StateObserver {

  @NonNull private final GoogleApiClient googleApiClient;
  @NonNull private final PowerManagerPreferences preferences;

  WearStateObserver(@NonNull Context context, @NonNull PowerManagerPreferences preferences) {
    super(context);
    this.preferences = preferences;
    googleApiClient =
        new GoogleApiClient.Builder(context.getApplicationContext()).addApiIfAvailable(Wearable.API)
            .build();
  }

  @WorkerThread @CheckResult private boolean isWearableNodeConnected() {
    final long waitTime = preferences.getWearableDelay();
    Timber.d("Wait for connection for %d seconds", waitTime);
    final NodeApi.GetConnectedNodesResult nodesResult =
        Wearable.NodeApi.getConnectedNodes(googleApiClient).await(waitTime, TimeUnit.SECONDS);
    Node wearableNode = null;
    final List<Node> nodes = nodesResult.getNodes();
    Timber.d("Search node list of size : %d", nodes.size());
    for (final Node node : nodes) {
      if (node.isNearby()) {
        Timber.d("Wearable node: %s %s", node.getDisplayName(), node.getId());
        wearableNode = node;
        break;
      }
    }

    final boolean result;
    if (wearableNode == null) {
      Timber.w("No wearable node was found");
      result = true;
    } else {
      Timber.d("Found a wearable node");
      result = false;
    }

    disconnectGoogleApiClient();
    return result;
  }

  /**
   * Return true if stream should continue, false otherwise
   */
  @WorkerThread @Override public boolean is() {
    Timber.d("Check if wearable is connected");
    final long waitTime = preferences.getWearableDelay();
    Timber.d("Wait for connection for %d seconds", waitTime);
    final ConnectionResult connectionResult =
        googleApiClient.blockingConnect(waitTime, TimeUnit.SECONDS);
    boolean result;
    if (connectionResult.isSuccess()) {
      Timber.d("Connect Google APIs");
      result = isWearableNodeConnected();
    } else {
      Timber.e("Could not connect to Google APIs");
{'fraction_non_alphanumeric': 0.09744531130487992, 'fraction_numerical': 0.002600581306409668, 'mean_word_length': 3.964312832194381, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5110841', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1796, 'n_words': 518}