@SuppressLint("NewApi")
  private void setNavigationBarTint(final int tintColor, boolean force) {
    if (mNavigationBarView == null) return;

    if (mSettingsHelper.shouldLinkStatusBarAndNavBar() && !force) {
      log("Ignoring manual navigation bar color change cause we're linked");
      return;
    }

    log("Setting navigation bar color to " + tintColor);

    if (mSettingsHelper.animateStatusBarTintChange()) {
      if (tintColor != KITKAT_TRANSPARENT_COLOR) {
        ValueAnimator colorAnimation =
            ValueAnimator.ofObject(new ArgbEvaluator(), mLastSetNavBarTint, tintColor);
        colorAnimation.addUpdateListener(
            new AnimatorUpdateListener() {
              @Override
              public void onAnimationUpdate(ValueAnimator animator) {
                mNavigationBarView.setBackgroundColor((Integer) animator.getAnimatedValue());
              }
            });
        colorAnimation.start();
      } else {
        mNavigationBarView.setBackgroundColor(KITKAT_TRANSPARENT_COLOR);
        Utils.setViewBackground(
            mNavigationBarView,
            new BarBackgroundDrawable(
                mStatusBarView.getContext(), mResources, R.drawable.nav_background));
      }
    } else {
      if (tintColor == KITKAT_TRANSPARENT_COLOR) {
        mNavigationBarView.setBackgroundColor(KITKAT_TRANSPARENT_COLOR);
        Utils.setViewBackground(
            mNavigationBarView,
            new BarBackgroundDrawable(
                mNavigationBarView.getContext(), mResources, R.drawable.nav_background));
      } else {
        mNavigationBarView.setBackgroundColor(tintColor);
      }
    }

    if (mNavigationBarView != null && tintColor != KITKAT_TRANSPARENT_COLOR) {
      Intent intent = new Intent("gravitybox.intent.action.ACTION_NAVBAR_CHANGED");
      intent.putExtra("navbarBgColor", tintColor);
      intent.putExtra("navbarColorEnable", true);
      mNavigationBarView.getContext().sendBroadcast(intent);
    }

    mLastSetNavBarTint = tintColor;
  }
  @SuppressLint("NewApi")
  public void setStatusBarTint(final int tintColor) {
    if (mStatusBarView == null) return;

    log("Setting statusbar color to " + tintColor);

    if (mSettingsHelper.animateStatusBarTintChange()) {
      int animateFrom =
          mLastSetColor == KITKAT_TRANSPARENT_COLOR ? Color.TRANSPARENT : mLastSetColor;
      int animateTo = tintColor == KITKAT_TRANSPARENT_COLOR ? Color.TRANSPARENT : tintColor;
      ValueAnimator colorAnimation =
          ValueAnimator.ofObject(new ArgbEvaluator(), animateFrom, animateTo);
      colorAnimation.addUpdateListener(
          new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
              mGradientDrawable.setColor((Integer) animator.getAnimatedValue());
            }
          });
      Utils.setViewBackground(mStatusBarView, mGradientDrawable);
      mGradientDrawable.setMode(mSettingsHelper.getOverlayMode());
      colorAnimation.start();
    } else {
      mStatusBarView.setAlpha(1f);
      if (tintColor == KITKAT_TRANSPARENT_COLOR) {
        Utils.setViewBackground(mStatusBarView, mGradientDrawable);
        mGradientDrawable.setColor(Color.TRANSPARENT);
        mGradientDrawable.setMode(mSettingsHelper.getOverlayMode());
      } else {
        Utils.setViewBackground(mStatusBarView, mGradientDrawable);
        mGradientDrawable.setColor(tintColor);
        mGradientDrawable.setMode(mSettingsHelper.getOverlayMode());
      }
    }

    mLastSetColor = tintColor;

    if (mSettingsHelper.shouldLinkStatusBarAndNavBar() && !mKeyboardUp) {
      mNavigationBarTint = tintColor;
      setNavigationBarTint(tintColor, true);
    }
  }
  private void resetToAutoDetect() {
    Editor editor = mSettingsHelper.getSharedPreferences().edit();

    editor.remove(
        SettingsHelper.getKeyName(mPackageName, mActivityName, SettingsKeys.STATUS_BAR_TINT));
    editor.remove(
        SettingsHelper.getKeyName(mPackageName, mActivityName, SettingsKeys.STATUS_BAR_ICON_TINT));
    editor.remove(
        SettingsHelper.getKeyName(mPackageName, mActivityName, SettingsKeys.NAVIGATION_BAR_TINT));
    editor.remove(
        SettingsHelper.getKeyName(
            mPackageName, mActivityName, SettingsKeys.NAVIGATION_BAR_ICON_TINT));
    editor.commit();

    mStatusBarTint = mSettingsHelper.getDefaultTint(Tint.STATUS_BAR, false);
    mStatusBarIconTint = mSettingsHelper.getDefaultTint(Tint.ICON, false);

    mStatusBarTintButton.setBackgroundColor(
        Color.parseColor(Utils.addHashIfNeeded(mStatusBarTint)));
    mStatusBarIconTintButton.setBackgroundColor(
        Color.parseColor(Utils.addHashIfNeeded(mStatusBarIconTint)));
  }
  @Override
  public void handleInitPackageResources(InitPackageResourcesParam resparam) throws Throwable {
    if (!resparam.packageName.equals("com.android.systemui")) return;

    try {
      // Before anything else, let's make sure we're not dealing with a Lenovo device
      // Lenovo is known for doing some deep customizations into UI, so let's just check
      // if is possible to hook a specific layout and work with it in that case
      String layout = "lenovo_gemini_super_status_bar";
      try {
        int resourceId = resparam.res.getIdentifier(layout, "layout", "com.android.systemui");
        if (resourceId == 0)
          layout = Utils.hasGeminiSupport() ? "gemini_super_status_bar" : "super_status_bar";
      } catch (Throwable t) {
        layout = Utils.hasGeminiSupport() ? "gemini_super_status_bar" : "super_status_bar";
      }

      StatusBarLayoutInflationHook hook = new StatusBarLayoutInflationHook(this);

      try {
        int resourceId =
            resparam.res.getIdentifier("msim_super_status_bar", "layout", "com.android.systemui");
        if (resourceId != 0)
          resparam.res.hookLayout("com.android.systemui", "layout", "msim_super_status_bar", hook);
      } catch (Throwable t) {
      }

      resparam.res.hookLayout("com.android.systemui", "layout", layout, hook);
    } catch (Throwable t) {
      log(t.getMessage());
    }

    qmemoButtonRESID =
        resparam.res.getIdentifier("navigation_button_qmemo", "id", "com.android.systemui");
    notificationButtonRESID =
        resparam.res.getIdentifier("navigation_button_notification", "id", "com.android.systemui");
  }
  private void setKitKatBatteryColor(int iconColor) {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) return;

    if (mKitKatBatteryView == null) return;

    boolean debug = mSettingsHelper.isDebugMode();

    try {
      final int[] colors = (int[]) XposedHelpers.getObjectField(mKitKatBatteryView, "mColors");
      colors[colors.length - 1] = iconColor;
      XposedHelpers.setObjectField(mKitKatBatteryView, "mColors", colors);
    } catch (NoSuchFieldError e) {
      if (debug) e.printStackTrace();
    }

    try {
      final Paint framePaint =
          (Paint) XposedHelpers.getObjectField(mKitKatBatteryView, "mFramePaint");
      framePaint.setColor(iconColor);
      framePaint.setAlpha(100);
    } catch (NoSuchFieldError e) {
      if (debug) e.printStackTrace();
    }

    try {
      final Paint boltPaint =
          (Paint) XposedHelpers.getObjectField(mKitKatBatteryView, "mBoltPaint");
      boltPaint.setColor(Utils.getIconColorForColor(iconColor, Color.BLACK, Color.WHITE, 0.7f));
      boltPaint.setAlpha(100);
    } catch (NoSuchFieldError e) {
      if (debug) e.printStackTrace();
    }

    try {
      XposedHelpers.setIntField(mKitKatBatteryView, "mChargeColor", iconColor);
    } catch (NoSuchFieldError e) {
      /* Beanstalk, not sure why the ROM changed this */
      try {
        XposedHelpers.setIntField(mKitKatBatteryView, "mBatteryColor", iconColor);
      } catch (NoSuchFieldError e1) {
      }
      if (debug) e.printStackTrace();
    }

    mKitKatBatteryView.invalidate();
  }
  @Override
  public void initZygote(StartupParam startupParam) throws Throwable {
    mStatusBarView = null;
    mSettingsHelper =
        new SettingsHelper(new XSharedPreferences(PackageNames.OURS, SettingsHelper.PREFS));

    mResources = XModuleResources.createInstance(startupParam.modulePath, null);

    findAndHookMethod(
        Activity.class,
        "onWindowFocusChanged",
        boolean.class,
        new OnWindowFocusedHook(mSettingsHelper, mResources));
    findAndHookMethod(
        Activity.class, "performResume", new ActivityOnResumeHook(mSettingsHelper, mResources));

    if (Utils.hasActionBar()) new ActionBarHooks(mSettingsHelper);

    // TODO: Fix this
    // WindowManagerServiceHooks.doHook(null);
    LGHooks.doImeHook();
  }
 public void setStatusBarView(View view) {
   mStatusBarView = view;
   Utils.setViewBackground(mStatusBarView, mGradientDrawable);
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_application_settings);

    mSettingsHelper = SettingsHelper.getInstance(getApplicationContext());

    Intent i = getIntent();
    parentIntent = i;

    setTitle(i.getStringExtra(Common.EXTRA_KEY_PACKAGE_FRIENDLY_NAME));

    String activityName = i.getStringExtra(Common.EXTRA_KEY_ACTIVITY_NAME);
    TextView currentActivity = (TextView) findViewById(R.id.currentActivity);
    if (!Common.EXTRA_KEY_VALUE_NONE.equals(activityName)) {
      currentActivity.setText(activityName);
      mActivityName = activityName;
    } else {
      currentActivity.setText(R.string.all_activities_title);
    }

    try {
      String packageName = i.getStringExtra(Common.EXTRA_KEY_PACKAGE_NAME);
      if (packageName.equals(PackageNames.LOCKSCREEN_STUB)) {
        mPackageName = packageName;
        if (Utils.hasActionBar())
          getActionBar().setIcon(getResources().getDrawable(R.drawable.ic_lock));
      } else {
        PackageManager pm = getPackageManager();
        ApplicationInfo app = pm.getApplicationInfo(packageName, 0);
        if (Utils.hasActionBar()) getActionBar().setIcon(app.loadIcon(pm));
        mPackageName = app.packageName;
      }
    } catch (NameNotFoundException e) {
      // Close the dialog gracefully, package might have been uninstalled
      finish();
      return;
    }

    if (Utils.hasActionBar()) {
      getActionBar().setDisplayShowCustomEnabled(true);
      getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    mStatusBarTint = mSettingsHelper.getTintColor(mPackageName, mActivityName, false);
    if (mStatusBarTint == null)
      mStatusBarTint = mSettingsHelper.getDefaultTint(Tint.STATUS_BAR, false);
    mStatusBarTintButton = (Button) findViewById(R.id.status_bar_tint_button);
    mStatusBarTintButton.setBackgroundColor(
        Color.parseColor(Utils.addHashIfNeeded(mStatusBarTint)));
    mStatusBarTintButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            onStatusBarTintColorButtonClicked();
          }
        });
    mStatusBarIconTint = mSettingsHelper.getIconColors(mPackageName, mActivityName, false);
    if (mStatusBarIconTint == null)
      mStatusBarIconTint = mSettingsHelper.getDefaultTint(Tint.ICON, false);

    mStatusBarIconTintButton = (Button) findViewById(R.id.icon_tint_button);
    mStatusBarIconTintButton.setBackgroundColor(
        Color.parseColor(Utils.addHashIfNeeded(mStatusBarIconTint)));
    mStatusBarIconTintButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            onStatusBarIconTintColorButtonClicked();
          }
        });
    mNavigationBarTint = mSettingsHelper.getNavigationBarTint(mPackageName, mActivityName, false);
    mNavigationBarIconTint =
        mSettingsHelper.getNavigationBarIconTint(mPackageName, activityName, false);

    mNavigationBarTintButton = (Button) findViewById(R.id.navigation_bar_tint_button);
    mNavigationBarIconTintButton = (Button) findViewById(R.id.navigation_bar_icon_tint_button);

    mNavigationBarTintButton.setBackgroundColor(
        Color.parseColor(Utils.addHashIfNeeded(mNavigationBarTint)));
    mNavigationBarTintButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            onNavigationBarTintButtonClicked();
          }
        });

    mNavigationBarIconTintButton.setBackgroundColor(
        Color.parseColor(Utils.addHashIfNeeded(mNavigationBarIconTint)));
    mNavigationBarIconTintButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            onNavigationBarIconTintButtonClicked();
          }
        });

    mResetToAutoDetectButton = (Button) findViewById(R.id.reset_to_auto_detect_button);
    mResetToAutoDetectButton.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View arg0) {
            resetToAutoDetect();
          }
        });

    mLinkPanelsCheckbox = (CheckBox) findViewById(R.id.link_panels_checkbox);
    mLinkPanelsCheckbox.setChecked(mSettingsHelper.shouldLinkPanels(mPackageName, mActivityName));
    mLinkPanelsCheckbox.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mSettingsHelper.setShouldLinkPanels(mPackageName, mActivityName, isChecked);
          }
        });

    mReactToActionBarCheckbox = (CheckBox) findViewById(R.id.react_actionbar_checkbox);
    mReactToActionBarCheckbox.setChecked(
        mSettingsHelper.shouldReactToActionBar(mPackageName, mActivityName));
    mReactToActionBarCheckbox.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mSettingsHelper.setShouldReactToActionBar(mPackageName, mActivityName, isChecked);
          }
        });

    mReverseTintActionBarChedkbox = (CheckBox) findViewById(R.id.reverse_tint_actionbar_checkbox);
    mReverseTintActionBarChedkbox.setChecked(
        mSettingsHelper.shouldReverseTintAbColor(mPackageName));
    mReverseTintActionBarChedkbox.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mSettingsHelper.setShouldReverseTintActionBar(mPackageName, mActivityName, isChecked);
          }
        });

    if (mActivityName != null) findViewById(R.id.package_specifc_options).setVisibility(View.GONE);
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_CANCELED) return;

    if (requestCode == STATUS_BAR_TINT_COLOR_REQUEST) {
      String newColor = Common.COLOR_A_SHADE_OF_GREY;
      if (data != null) {
        if (data.hasExtra("color")) newColor = data.getStringExtra("color");
      }
      if ("#0".equals(newColor)) {
        newColor = "#00000000";
      }
      mStatusBarTint = newColor;
      try {
        int color = Color.parseColor(Utils.addHashIfNeeded(newColor));
        mStatusBarTintButton.setBackgroundColor(color);
        mSettingsHelper.setStatusBarTintColor(mPackageName, mActivityName, newColor);
      } catch (IllegalArgumentException e) {
        Toast.makeText(getApplicationContext(), R.string.invalid_color, Toast.LENGTH_SHORT).show();
      }
    } else if (requestCode == STATUS_BAR_ICON_TINT_COLOR_REQUEST) {
      String newColor = Common.COLOR_WHITE;
      if (data != null) {
        if (data.hasExtra("color")) newColor = data.getStringExtra("color");
      }
      if ("#0".equals(newColor)) {
        newColor = "#00ffffff";
      }
      mStatusBarIconTint = newColor;
      try {
        int color = Color.parseColor(Utils.addHashIfNeeded(newColor));
        mStatusBarIconTintButton.setBackgroundColor(color);
        mSettingsHelper.setIconColors(mPackageName, mActivityName, newColor);
      } catch (IllegalArgumentException e) {
        Toast.makeText(getApplicationContext(), R.string.invalid_color, Toast.LENGTH_SHORT).show();
      }
    } else if (requestCode == NAVIGATION_BAR_TINT_COLOR_REQUEST) {
      String newColor = Common.COLOR_BLACK;
      if (data != null) {
        if (data.hasExtra("color")) newColor = data.getStringExtra("color");
      }
      if ("#0".equals(newColor)) {
        newColor = "#00000000";
      }
      mStatusBarTint = newColor;
      try {
        int color = Color.parseColor(Utils.addHashIfNeeded(newColor));
        mNavigationBarTintButton.setBackgroundColor(color);
        mSettingsHelper.setTintColor(Tint.NAV_BAR, mPackageName, mActivityName, newColor);
      } catch (IllegalArgumentException e) {
        Toast.makeText(getApplicationContext(), R.string.invalid_color, Toast.LENGTH_SHORT).show();
      }
    } else if (requestCode == NAVIGATION_BAR_ICON_TINT_COLOR_REQUEST) {
      String newColor = Common.COLOR_WHITE;
      if (data != null) {
        if (data.hasExtra("color")) newColor = data.getStringExtra("color");
      }
      if ("#0".equals(newColor)) {
        newColor = "#00000000";
      }
      mStatusBarTint = newColor;
      try {
        int color = Color.parseColor(Utils.addHashIfNeeded(newColor));
        mNavigationBarIconTintButton.setBackgroundColor(color);
        mSettingsHelper.setTintColor(Tint.NAV_BAR_ICON, mPackageName, mActivityName, newColor);
      } catch (IllegalArgumentException e) {
        Toast.makeText(getApplicationContext(), R.string.invalid_color, Toast.LENGTH_SHORT).show();
      }
    }
  }