<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.app.service;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.widget.ImageButton;
import android.widget.ImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.pyamsoft.powermanager.PowerManager;
import com.pyamsoft.powermanager.R;
import com.pyamsoft.powermanager.app.main.MainActivity;
import com.pyamsoft.powermanager.app.manager.backend.ManagerBluetooth;
import com.pyamsoft.powermanager.app.manager.backend.ManagerData;
import com.pyamsoft.powermanager.app.manager.backend.ManagerSync;
import com.pyamsoft.powermanager.app.manager.backend.ManagerWifi;
import com.pyamsoft.powermanager.dagger.observer.manage.BluetoothManageObserver;
import com.pyamsoft.powermanager.dagger.observer.manage.DaggerManageObserverComponent;
import com.pyamsoft.powermanager.dagger.observer.manage.DataManageObserver;
import com.pyamsoft.powermanager.dagger.observer.manage.SyncManageObserver;
import com.pyamsoft.powermanager.dagger.observer.manage.WifiManageObserver;
import com.pyamsoft.powermanager.dagger.observer.state.BluetoothStateObserver;
import com.pyamsoft.powermanager.dagger.observer.state.DaggerStateObserverComponent;
import com.pyamsoft.powermanager.dagger.observer.state.DataStateObserver;
import com.pyamsoft.powermanager.dagger.observer.state.SyncStateObserver;
import com.pyamsoft.powermanager.dagger.observer.state.WifiStateObserver;
import com.pyamsoft.powermanager.dagger.service.DaggerFullNotificationComponent;
import com.pyamsoft.pydroid.model.AsyncDrawable;
import com.pyamsoft.pydroid.tool.AsyncVectorDrawableTask;
import com.pyamsoft.pydroid.util.AppUtil;
import com.pyamsoft.pydroid.util.DrawableUtil;
import javax.inject.Inject;
import timber.log.Timber;

public class FullNotificationActivity extends AppCompatActivity
    implements FullNotificationPresenter.FullNotificationView {

  @Inject FullNotificationPresenter presenter;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DaggerFullNotificationComponent.builder()
        .powerManagerComponent(PowerManager.getInstance().getPowerManagerComponent())
        .build()
        .inject(this);

    presenter.bindView(this);
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    presenter.unbindView();
  }

  @Override protected void onResume() {
    super.onResume();

    Timber.d("onResume");
    presenter.onResume();
  }

  @Override protected void onPause() {
    super.onPause();

    Timber.d("onPause");
    presenter.onPause();
  }

  @Override protected void onPostResume() {
    super.onPostResume();
    Timber.d("Show Full Notification");
    AppUtil.guaranteeSingleDialogFragment(getSupportFragmentManager(), new FullDialog(),
        "full_dialog");
  }

  @Override public void onDismissEvent() {
    Timber.d("Full Notification Dismissed");
    finish();
  }

  public static final class FullDialog extends DialogFragment
      implements WifiStateObserver.View, DataStateObserver.View, BluetoothStateObserver.View,
      SyncStateObserver.View, WifiManageObserver.View, DataManageObserver.View,
      BluetoothManageObserver.View, SyncManageObserver.View {

    @Inject ManagerWifi managerWifi;
    @Inject ManagerData managerData;
    @Inject ManagerBluetooth managerBluetooth;
    @Inject ManagerSync managerSync;

    @BindView(R.id.full_notification_wifi_manage) SwitchCompat wifiManage;
    @BindView(R.id.full_notification_wifi_toggle) ImageButton wifiToggle;
    @BindView(R.id.full_notification_bluetooth_manage) SwitchCompat bluetoothManage;
    @BindView(R.id.full_notification_bluetooth_toggle) ImageButton bluetoothToggle;
    @BindView(R.id.full_notification_data_manage) SwitchCompat dataManage;
    @BindView(R.id.full_notification_data_toggle) ImageButton dataToggle;
    @BindView(R.id.full_notification_sync_manage) SwitchCompat syncManage;
    @BindView(R.id.full_notification_sync_toggle) ImageButton syncToggle;

    @BindView(R.id.full_notification_main) ImageView mainButton;
    @BindView(R.id.full_notification_close) ImageView closeButton;

    @Inject WifiStateObserver wifiStateObserver;
    @Inject DataStateObserver dataStateObserver;
    @Inject BluetoothStateObserver bluetoothStateObserver;
    @Inject SyncStateObserver syncStateObserver;

    @Inject WifiManageObserver wifiManageObserver;
    @Inject DataManageObserver dataManageObserver;
    @Inject BluetoothManageObserver bluetoothManageObserver;
    @Inject SyncManageObserver syncManageObserver;

    private Unbinder unbinder;
    private AsyncVectorDrawableTask mainTask;
    private AsyncVectorDrawableTask closeTask;

    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setCancelable(true);
      Timber.d("onCreate");

      DaggerStateObserverComponent.builder()
          .powerManagerComponent(PowerManager.getInstance().getPowerManagerComponent())
          .build()
          .inject(this);

      DaggerManageObserverComponent.builder()
          .powerManagerComponent(PowerManager.getInstance().getPowerManagerComponent())
          .build()
          .inject(this);

      wifiStateObserver.setView(this);
      dataStateObserver.setView(this);
      bluetoothStateObserver.setView(this);
      syncStateObserver.setView(this);

      wifiManageObserver.setView(this);
      dataManageObserver.setView(this);
      bluetoothManageObserver.setView(this);
      syncManageObserver.setView(this);

      wifiStateObserver.register();
      dataStateObserver.register();
      bluetoothStateObserver.register();
      syncStateObserver.register();

      wifiManageObserver.register();
      dataManageObserver.register();
      bluetoothManageObserver.register();
      syncManageObserver.register();
    }

    @Override public void onDestroy() {
      super.onDestroy();
      Timber.d("onDestroy");

      managerWifi.cleanup();
      managerData.cleanup();
      managerBluetooth.cleanup();
      managerSync.cleanup();

      wifiStateObserver.unregister();
      dataStateObserver.unregister();
      bluetoothStateObserver.unregister();
      syncStateObserver.unregister();

      cancelTask(mainTask);
      cancelTask(closeTask);

      unbinder.unbind();
    }

    private void cancelTask(@Nullable AsyncTask task) {
      if (task != null) {
        if (!task.isCancelled()) {
          Timber.d("Cancel task");<commit_msg>Unregister the other observers<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.app.service;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SwitchCompat;
import android.view.LayoutInflater;
import android.widget.ImageButton;
import android.widget.ImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import com.pyamsoft.powermanager.PowerManager;
import com.pyamsoft.powermanager.R;
import com.pyamsoft.powermanager.app.main.MainActivity;
import com.pyamsoft.powermanager.app.manager.backend.ManagerBluetooth;
import com.pyamsoft.powermanager.app.manager.backend.ManagerData;
import com.pyamsoft.powermanager.app.manager.backend.ManagerSync;
import com.pyamsoft.powermanager.app.manager.backend.ManagerWifi;
import com.pyamsoft.powermanager.dagger.observer.manage.BluetoothManageObserver;
import com.pyamsoft.powermanager.dagger.observer.manage.DaggerManageObserverComponent;
import com.pyamsoft.powermanager.dagger.observer.manage.DataManageObserver;
import com.pyamsoft.powermanager.dagger.observer.manage.SyncManageObserver;
import com.pyamsoft.powermanager.dagger.observer.manage.WifiManageObserver;
import com.pyamsoft.powermanager.dagger.observer.state.BluetoothStateObserver;
import com.pyamsoft.powermanager.dagger.observer.state.DaggerStateObserverComponent;
import com.pyamsoft.powermanager.dagger.observer.state.DataStateObserver;
import com.pyamsoft.powermanager.dagger.observer.state.SyncStateObserver;
import com.pyamsoft.powermanager.dagger.observer.state.WifiStateObserver;
import com.pyamsoft.powermanager.dagger.service.DaggerFullNotificationComponent;
import com.pyamsoft.pydroid.model.AsyncDrawable;
import com.pyamsoft.pydroid.tool.AsyncVectorDrawableTask;
import com.pyamsoft.pydroid.util.AppUtil;
import com.pyamsoft.pydroid.util.DrawableUtil;
import javax.inject.Inject;
import timber.log.Timber;

public class FullNotificationActivity extends AppCompatActivity
    implements FullNotificationPresenter.FullNotificationView {

  @Inject FullNotificationPresenter presenter;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DaggerFullNotificationComponent.builder()
        .powerManagerComponent(PowerManager.getInstance().getPowerManagerComponent())
        .build()
        .inject(this);

    presenter.bindView(this);
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    presenter.unbindView();
  }

  @Override protected void onResume() {
    super.onResume();

    Timber.d("onResume");
    presenter.onResume();
  }

  @Override protected void onPause() {
    super.onPause();

    Timber.d("onPause");
    presenter.onPause();
  }

  @Override protected void onPostResume() {
    super.onPostResume();
    Timber.d("Show Full Notification");
    AppUtil.guaranteeSingleDialogFragment(getSupportFragmentManager(), new FullDialog(),
        "full_dialog");
  }

  @Override public void onDismissEvent() {
    Timber.d("Full Notification Dismissed");
    finish();
  }

  public static final class FullDialog extends DialogFragment
      implements WifiStateObserver.View, DataStateObserver.View, BluetoothStateObserver.View,
      SyncStateObserver.View, WifiManageObserver.View, DataManageObserver.View,
      BluetoothManageObserver.View, SyncManageObserver.View {

    @Inject ManagerWifi managerWifi;
    @Inject ManagerData managerData;
    @Inject ManagerBluetooth managerBluetooth;
    @Inject ManagerSync managerSync;

    @BindView(R.id.full_notification_wifi_manage) SwitchCompat wifiManage;
    @BindView(R.id.full_notification_wifi_toggle) ImageButton wifiToggle;
    @BindView(R.id.full_notification_bluetooth_manage) SwitchCompat bluetoothManage;
    @BindView(R.id.full_notification_bluetooth_toggle) ImageButton bluetoothToggle;
    @BindView(R.id.full_notification_data_manage) SwitchCompat dataManage;
    @BindView(R.id.full_notification_data_toggle) ImageButton dataToggle;
    @BindView(R.id.full_notification_sync_manage) SwitchCompat syncManage;
    @BindView(R.id.full_notification_sync_toggle) ImageButton syncToggle;

    @BindView(R.id.full_notification_main) ImageView mainButton;
    @BindView(R.id.full_notification_close) ImageView closeButton;

    @Inject WifiStateObserver wifiStateObserver;
    @Inject DataStateObserver dataStateObserver;
    @Inject BluetoothStateObserver bluetoothStateObserver;
    @Inject SyncStateObserver syncStateObserver;

    @Inject WifiManageObserver wifiManageObserver;
    @Inject DataManageObserver dataManageObserver;
    @Inject BluetoothManageObserver bluetoothManageObserver;
    @Inject SyncManageObserver syncManageObserver;

    private Unbinder unbinder;
    private AsyncVectorDrawableTask mainTask;
    private AsyncVectorDrawableTask closeTask;

    @Override public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setCancelable(true);
      Timber.d("onCreate");

      DaggerStateObserverComponent.builder()
          .powerManagerComponent(PowerManager.getInstance().getPowerManagerComponent())
          .build()
          .inject(this);

      DaggerManageObserverComponent.builder()
          .powerManagerComponent(PowerManager.getInstance().getPowerManagerComponent())
          .build()
          .inject(this);

      wifiStateObserver.setView(this);
      dataStateObserver.setView(this);
      bluetoothStateObserver.setView(this);
      syncStateObserver.setView(this);

      wifiManageObserver.setView(this);
      dataManageObserver.setView(this);
      bluetoothManageObserver.setView(this);
      syncManageObserver.setView(this);

      wifiStateObserver.register();
      dataStateObserver.register();
      bluetoothStateObserver.register();
      syncStateObserver.register();

      wifiManageObserver.register();
      dataManageObserver.register();
      bluetoothManageObserver.register();
      syncManageObserver.register();
    }

    @Override public void onDestroy() {
      super.onDestroy();
      Timber.d("onDestroy");

      managerWifi.cleanup();
      managerData.cleanup();
      managerBluetooth.cleanup();
      managerSync.cleanup();

      wifiStateObserver.unregister();
      dataStateObserver.unregister();
      bluetoothStateObserver.unregister();
      syncStateObserver.unregister();

      wifiManageObserver.unregister();
      dataManageObserver.unregister();
      bluetoothManageObserver.unregister();
      syncManageObserver.unregister();

      cancelTask(mainTask);
      cancelTask(closeTask);

      unbinder.unbind();
    }

    private void cancelTask(@Nullable AsyncTask task) {
      if (task != null) {
        if (!task.isCancelled()) {
          Timber.d("Cancel task");
{'fraction_non_alphanumeric': 0.09113825867752569, 'fraction_numerical': 0.001680563635188417, 'mean_word_length': 4.831888428194497, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6443519', 'n_tokens_mistral': 4415, 'n_tokens_neox': 4314, 'n_words': 780}