<commit_before>/*
 * Copyright (C) 2013 <NAME> for GravityBox Project (C3C076@xda)
 * 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.ceco.kitkat.gravitybox;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XSharedPreferences;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_InitPackageResources.InitPackageResourcesParam;
import de.robv.android.xposed.callbacks.XC_LayoutInflated;

public class ModBatteryStyle {
    private static final String TAG = "GB:ModBatteryStyle";
    public static final String PACKAGE_NAME = "com.android.systemui";
    public static final String CLASS_PHONE_STATUSBAR = "com.android.systemui.statusbar.phone.PhoneStatusBar";
    public static final String CLASS_BATTERY_CONTROLLER = 
            "com.android.systemui.statusbar.policy.BatteryController";
    private static final boolean DEBUG = false;

    private static final String ACTION_MTK_BATTERY_PERCENTAGE_SWITCH = 
            "mediatek.intent.action.BATTERY_PERCENTAGE_SWITCH";
    public static final String EXTRA_MTK_BATTERY_PERCENTAGE_STATE = "state";
    public static final String SETTING_MTK_BATTERY_PERCENTAGE = "battery_percentage";

    private static int mBatteryStyle;
    private static boolean mBatteryPercentTextEnabled;
    private static boolean mMtkPercentTextEnabled;
    private static StatusbarBatteryPercentage mPercentText;
    private static int mPercentTextSize;
    private static String mPercentSign;
    private static CmCircleBattery mCircleBattery;
    private static View mStockBattery;
    private static KitKatBattery mKitKatBattery;

    private static void log(String message) {
        XposedBridge.log(TAG + ": " + message);
    }

    private static BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_BATTERY_STYLE_CHANGED) &&
                    intent.hasExtra(GravityBoxSettings.EXTRA_BATTERY_STYLE)) {
                        mBatteryStyle = intent.getIntExtra(GravityBoxSettings.EXTRA_BATTERY_STYLE, 1);
                        if (DEBUG) log("mBatteryStyle changed to: " + mBatteryStyle);
                        updateBatteryStyle();
            } else if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_BATTERY_PERCENT_TEXT_CHANGED) &&
                    intent.hasExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT)) {
                        mBatteryPercentTextEnabled = intent.getBooleanExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT, false);
                        if (DEBUG) log("mPercentText changed to: " + mBatteryPercentTextEnabled);
                        updatePercentText(null, false, false);
            } else if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_BATTERY_PERCENT_TEXT_SIZE_CHANGED) &&
                    intent.hasExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT_SIZE)) {
                        mPercentTextSize = intent.getIntExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT_SIZE, 16);
                        if (DEBUG) log("mPercentTextSize changed to: " + mPercentTextSize);
                        updatePercentText(null, true, false);
            } else if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_BATTERY_PERCENT_TEXT_STYLE_CHANGED) &&
                    intent.hasExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT_STYLE)) {
                        mPercentSign = intent.getStringExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT_STYLE);
                        if (DEBUG) log("mPercentSign changed to: " + mPercentSign);
                        updatePercentText(null, false, true);
            } else if (intent.getAction().equals(ACTION_MTK_BATTERY_PERCENTAGE_SWITCH)) {
                mMtkPercentTextEnabled = intent.getIntExtra(EXTRA_MTK_BATTERY_PERCENTAGE_STATE, 0) == 1;
                if (DEBUG) log("mMtkPercentText changed to: " + mMtkPercentTextEnabled);
                updatePercentText(null, false, false);
            }
        }
    };

    public static void initResources(XSharedPreferences prefs, InitPackageResourcesParam resparam) {
        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{
                resparam.res.hookLayout(PACKAGE_NAME, "layout", layout, new XC_LayoutInflated() {

                    @Override
                    public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {
                        if (DEBUG) log("Lenovo custom layout found");
                    }
                });
            } catch (Throwable t) {
                // Specific layout not found, so let's work with standard layout 
                layout = Utils.hasGeminiSupport() ? "gemini_super_status_bar" : "super_status_bar";
            } 
            final String[] batteryPercentTextIds = new String[] { "percentage", "battery_text" };

            resparam.res.hookLayout(PACKAGE_NAME, "layout", layout, new XC_LayoutInflated() {

                @Override
                public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {

                    ViewGroup vg = (ViewGroup) liparam.view.findViewById(
                            liparam.res.getIdentifier("signal_battery_cluster", "id", PACKAGE_NAME));

                    // inject percent text if it doesn't exist
                    for (String bptId : batteryPercentTextIds) {
                        final int bptResId = liparam.res.getIdentifier(
                                bptId, "id", PACKAGE_NAME);
                        if (bptResId != 0) {
                            View v = vg.findViewById(bptResId);
                            if (v != null && v instanceof TextView) {
                                mPercentText = new StatusbarBatteryPercentage((TextView) v);
                                mPercentText.getView().setTag("percentage");
                                if (DEBUG) log("Battery percent text found as: " + bptId);
                                break;
                            }
                        }
                    }
                    if (mPercentText == null) {
                        TextView percentTextView = new TextView(vg.getContext());
                        percentTextView.setTag("percentage");
                        LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        percentTextView.setLayoutParams(lParams);
                        percentTextView.setPadding(6, 0, 0, 0);
                        percentTextView.setTextSize(1, 16);
                        percentTextView.setTextColor(Color.WHITE);
                        percentTextView.setVisibility(View.GONE);
                        mPercentText = new StatusbarBatteryPercentage(percentTextView);
                        vg.addView(mPercentText.getView(), vg.getChildCount()-1);
                        if (DEBUG) log("Battery percent text injected");
                    }
                    ModStatusbarColor.registerIconManagerListener(mPercentText);

                    // inject circle battery view
                    mCircleBattery = new CmCircleBattery(vg.getContext());
                    mCircleBattery.setTag("circle_battery");
                    LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    lParams.gravity = Gravity.CENTER_VERTICAL;
                    mCircleBattery.setLayoutParams(lParams);
                    mCircleBattery.setPadding(4, 0, 0, 0);
                    mCircleBattery.setVisibility(View.GONE);
                    ModStatusbarColor.registerIconManagerListener(mCircleBattery);
                    vg.addView(mCircleBattery);
                    if (DEBUG) log("CmCircleBattery injected");

                    // inject KitKat battery view
                    mKitKatBattery = new KitKatBattery(vg.getContext());
                    mKitKatBattery.setTag("kitkat_battery");
                    final float density = liparam.res.getDisplayMetrics().density;
                    lParams = new LinearLayout.LayoutParams((int)(density * 10.5f), 
                            (int)(density * 16));
                    lParams.setMarginStart((int)(density * 4));
                    lParams.bottomMargin = 2;
                    mKitKatBattery.setLayoutParams(lParams);
                    mKitKatBattery.setVisibility(View.GONE);
                    ModStatusbarColor.registerIconManagerListener(mKitKatBattery);
                    vg.addView(mKitKatBattery);

                    // find battery
                    mStockBattery = vg.findViewById(
                            liparam.res.getIdentifier("battery", "id", PACKAGE_NAME));
                    if (mStockBattery != null) {
                        mStockBattery.setTag("stock_battery");
                        StatusbarBattery sbb = new StatusbarBattery(mStockBattery);
                        ModStatusbarColor.registerIconManagerListener(sbb);
                    }
                }
                
            });
        } catch (Throwable t) {<commit_msg>Increased left margin of circle battery<commit_after>/*
 * Copyright (C) 2013 <NAME> for GravityBox Project (C<EMAIL>)
 * 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.ceco.kitkat.gravitybox;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XSharedPreferences;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_InitPackageResources.InitPackageResourcesParam;
import de.robv.android.xposed.callbacks.XC_LayoutInflated;

public class ModBatteryStyle {
    private static final String TAG = "GB:ModBatteryStyle";
    public static final String PACKAGE_NAME = "com.android.systemui";
    public static final String CLASS_PHONE_STATUSBAR = "com.android.systemui.statusbar.phone.PhoneStatusBar";
    public static final String CLASS_BATTERY_CONTROLLER = 
            "com.android.systemui.statusbar.policy.BatteryController";
    private static final boolean DEBUG = false;

    private static final String ACTION_MTK_BATTERY_PERCENTAGE_SWITCH = 
            "mediatek.intent.action.BATTERY_PERCENTAGE_SWITCH";
    public static final String EXTRA_MTK_BATTERY_PERCENTAGE_STATE = "state";
    public static final String SETTING_MTK_BATTERY_PERCENTAGE = "battery_percentage";

    private static int mBatteryStyle;
    private static boolean mBatteryPercentTextEnabled;
    private static boolean mMtkPercentTextEnabled;
    private static StatusbarBatteryPercentage mPercentText;
    private static int mPercentTextSize;
    private static String mPercentSign;
    private static CmCircleBattery mCircleBattery;
    private static View mStockBattery;
    private static KitKatBattery mKitKatBattery;

    private static void log(String message) {
        XposedBridge.log(TAG + ": " + message);
    }

    private static BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_BATTERY_STYLE_CHANGED) &&
                    intent.hasExtra(GravityBoxSettings.EXTRA_BATTERY_STYLE)) {
                        mBatteryStyle = intent.getIntExtra(GravityBoxSettings.EXTRA_BATTERY_STYLE, 1);
                        if (DEBUG) log("mBatteryStyle changed to: " + mBatteryStyle);
                        updateBatteryStyle();
            } else if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_BATTERY_PERCENT_TEXT_CHANGED) &&
                    intent.hasExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT)) {
                        mBatteryPercentTextEnabled = intent.getBooleanExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT, false);
                        if (DEBUG) log("mPercentText changed to: " + mBatteryPercentTextEnabled);
                        updatePercentText(null, false, false);
            } else if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_BATTERY_PERCENT_TEXT_SIZE_CHANGED) &&
                    intent.hasExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT_SIZE)) {
                        mPercentTextSize = intent.getIntExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT_SIZE, 16);
                        if (DEBUG) log("mPercentTextSize changed to: " + mPercentTextSize);
                        updatePercentText(null, true, false);
            } else if (intent.getAction().equals(GravityBoxSettings.ACTION_PREF_BATTERY_PERCENT_TEXT_STYLE_CHANGED) &&
                    intent.hasExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT_STYLE)) {
                        mPercentSign = intent.getStringExtra(GravityBoxSettings.EXTRA_BATTERY_PERCENT_TEXT_STYLE);
                        if (DEBUG) log("mPercentSign changed to: " + mPercentSign);
                        updatePercentText(null, false, true);
            } else if (intent.getAction().equals(ACTION_MTK_BATTERY_PERCENTAGE_SWITCH)) {
                mMtkPercentTextEnabled = intent.getIntExtra(EXTRA_MTK_BATTERY_PERCENTAGE_STATE, 0) == 1;
                if (DEBUG) log("mMtkPercentText changed to: " + mMtkPercentTextEnabled);
                updatePercentText(null, false, false);
            }
        }
    };

    public static void initResources(XSharedPreferences prefs, InitPackageResourcesParam resparam) {
        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{
                resparam.res.hookLayout(PACKAGE_NAME, "layout", layout, new XC_LayoutInflated() {

                    @Override
                    public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {
                        if (DEBUG) log("Lenovo custom layout found");
                    }
                });
            } catch (Throwable t) {
                // Specific layout not found, so let's work with standard layout 
                layout = Utils.hasGeminiSupport() ? "gemini_super_status_bar" : "super_status_bar";
            } 
            final String[] batteryPercentTextIds = new String[] { "percentage", "battery_text" };

            resparam.res.hookLayout(PACKAGE_NAME, "layout", layout, new XC_LayoutInflated() {

                @Override
                public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {

                    ViewGroup vg = (ViewGroup) liparam.view.findViewById(
                            liparam.res.getIdentifier("signal_battery_cluster", "id", PACKAGE_NAME));

                    // inject percent text if it doesn't exist
                    for (String bptId : batteryPercentTextIds) {
                        final int bptResId = liparam.res.getIdentifier(
                                bptId, "id", PACKAGE_NAME);
                        if (bptResId != 0) {
                            View v = vg.findViewById(bptResId);
                            if (v != null && v instanceof TextView) {
                                mPercentText = new StatusbarBatteryPercentage((TextView) v);
                                mPercentText.getView().setTag("percentage");
                                if (DEBUG) log("Battery percent text found as: " + bptId);
                                break;
                            }
                        }
                    }
                    if (mPercentText == null) {
                        TextView percentTextView = new TextView(vg.getContext());
                        percentTextView.setTag("percentage");
                        LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                        percentTextView.setLayoutParams(lParams);
                        percentTextView.setPadding(6, 0, 0, 0);
                        percentTextView.setTextSize(1, 16);
                        percentTextView.setTextColor(Color.WHITE);
                        percentTextView.setVisibility(View.GONE);
                        mPercentText = new StatusbarBatteryPercentage(percentTextView);
                        vg.addView(mPercentText.getView(), vg.getChildCount()-1);
                        if (DEBUG) log("Battery percent text injected");
                    }
                    ModStatusbarColor.registerIconManagerListener(mPercentText);

                    // inject circle battery view
                    mCircleBattery = new CmCircleBattery(vg.getContext());
                    mCircleBattery.setTag("circle_battery");
                    LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    lParams.gravity = Gravity.CENTER_VERTICAL;
                    mCircleBattery.setLayoutParams(lParams);
                    mCircleBattery.setPadding(6, 0, 0, 0);
                    mCircleBattery.setVisibility(View.GONE);
                    ModStatusbarColor.registerIconManagerListener(mCircleBattery);
                    vg.addView(mCircleBattery);
                    if (DEBUG) log("CmCircleBattery injected");

                    // inject KitKat battery view
                    mKitKatBattery = new KitKatBattery(vg.getContext());
                    mKitKatBattery.setTag("kitkat_battery");
                    final float density = liparam.res.getDisplayMetrics().density;
                    lParams = new LinearLayout.LayoutParams((int)(density * 10.5f), 
                            (int)(density * 16));
                    lParams.setMarginStart((int)(density * 4));
                    lParams.bottomMargin = 2;
                    mKitKatBattery.setLayoutParams(lParams);
                    mKitKatBattery.setVisibility(View.GONE);
                    ModStatusbarColor.registerIconManagerListener(mKitKatBattery);
                    vg.addView(mKitKatBattery);

                    // find battery
                    mStockBattery = vg.findViewById(
                            liparam.res.getIdentifier("battery", "id", PACKAGE_NAME));
                    if (mStockBattery != null) {
                        mStockBattery.setTag("stock_battery");
                        StatusbarBattery sbb = new StatusbarBattery(mStockBattery);
                        ModStatusbarColor.registerIconManagerListener(sbb);
                    }
                }
                
            });
        } catch (Throwable t) {
{'fraction_non_alphanumeric': 0.07672454246832473, 'fraction_numerical': 0.00328484279680901, 'mean_word_length': 2.1515823720792664, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2762404', 'n_tokens_mistral': 5787, 'n_tokens_neox': 5405, 'n_words': 1159}