<commit_before>package com.pddstudio.otgsubs.fragments;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.pddstudio.otgsubs.R;
import com.pddstudio.otgsubs.beans.EventBusBean;
import com.pddstudio.otgsubs.beans.ManifestProcessorBean;
import com.pddstudio.otgsubs.events.ColorChooserDialogEvent;
import com.pddstudio.otgsubs.events.PatchThemeDialogEvent;
import com.pddstudio.otgsubs.models.ColorChooserType;
import com.pddstudio.otgsubs.services.PatchTemplateService;
import com.pddstudio.substratum.packager.models.ApkInfo;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.List;

/**
 * Created by pddstudio on 26/04/2017.
 */

@EFragment(R.layout.fragment_theme_patcher)
public class ThemePatcherFragment extends Fragment {

	public static final String TAG = ThemePatcherFragment.class.getSimpleName();

	public static ThemePatcherFragment newInstance(ApkInfo apkInfo) {
		return ThemePatcherFragment_.builder().apkInfo(apkInfo).build();
	}

	@FragmentArg
	protected ApkInfo apkInfo;

	@Bean
	protected EventBusBean eventBus;

	@Bean
	protected ManifestProcessorBean manifestProcessor;

	@ViewById(R.id.primary_color_dark_btn)
	protected Button primaryDarkColorButton;

	@ViewById(R.id.primary_color_btn)
	protected Button primaryColorButton;

	@ViewById(R.id.primary_color_dark_img)
	protected ImageView primaryColorDarkImage;

	@ViewById(R.id.primary_color_img)
	protected ImageView primaryColorImage;

	@ViewById(R.id.theme_name_input)
	protected EditText themeNameEditText;

	private HashMap<String, String> userMappings = new HashMap<>();

	@Subscribe
	public void onColorSelectionCompleted(ColorChooserDialogEvent event) {
		if(event != null && !event.isOpenRequest()) {
			String resultColor = event.getResultColor();
			Log.d(TAG, "Selected Color: " + resultColor + " type: " + event.getColorChooserType());
			switch (event.getColorChooserType()) {
				case IGNORE:
					default:
						return;
				case PRIMARY_COLOR:
					GradientDrawable bgShape = (GradientDrawable) primaryColorImage.getDrawable();
					bgShape.setColor(Color.parseColor(resultColor));
					primaryColorButton.setText(getString(R.string.patcher_btn_primary_color, resultColor));
					userMappings.put("primaryColor", resultColor);
					break;
				case PRIMARY_DARK_COLOR:
					GradientDrawable bgShape2 = (GradientDrawable) primaryColorDarkImage.getDrawable();
					bgShape2.setColor(Color.parseColor(resultColor));
					primaryDarkColorButton.setText(getString(R.string.patcher_btn_primary_dark_color, resultColor));
					userMappings.put("primaryColorDark", resultColor);
					break;
			}
		}
	}

	@AfterInject
	protected void prepareManifest() {
		List<ApkInfo> apkInfos = manifestProcessor.getSupportedThemes();
		Toast.makeText(this.getContext(), "Compatible themes found: " + apkInfos.size(), Toast.LENGTH_LONG).show();
	}

	@Override
	public void onStart() {
		super.onStart();
		eventBus.register(this);
	}

	@Override
	public void onStop() {
		eventBus.unregister(this);
		super.onStop();
	}

	@Click({R.id.primary_color_dark_btn, R.id.primary_color_btn})
	protected void openColorPickerDialog(View view) {
		boolean isForPrimaryColor = view.getId() == R.id.primary_color_btn;
		ColorChooserDialogEvent colorChooserDialogEvent = new ColorChooserDialogEvent(true, isForPrimaryColor ? ColorChooserType.PRIMARY_COLOR : ColorChooserType.PRIMARY_DARK_COLOR);
		eventBus.post(colorChooserDialogEvent);
	}

	@Click(R.id.build_patched_theme_btn)
	protected void onBuildPatchedTheme() {
		eventBus.post(new PatchThemeDialogEvent(true));
		PatchTemplateService.patchTargetTheme(getContext(), apkInfo, themeNameEditText.getText().toString(), userMappings);
	}

}<commit_msg>Started new implementation in ThemePatcherFragment<commit_after>package com.pddstudio.otgsubs.fragments;

import android.support.v4.app.Fragment;
import android.util.Log;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.afollestad.materialdialogs.MaterialDialog;
import com.pddstudio.otgsubs.R;
import com.pddstudio.otgsubs.beans.EventBusBean;
import com.pddstudio.otgsubs.beans.ManifestProcessorBean;
import com.pddstudio.otgsubs.events.ColorChooserDialogEvent;
import com.pddstudio.otgsubs.events.PatchThemeDialogEvent;
import com.pddstudio.otgsubs.events.PatchThemePreparationEvent;
import com.pddstudio.otgsubs.services.PatchTemplateService;
import com.pddstudio.otgsubs.views.TemplateItemView;
import com.pddstudio.substratum.packager.models.ApkInfo;
import com.pddstudio.substratum.template.patcher.TemplateConfiguration;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;

import java8.util.stream.StreamSupport;

/**
 * Created by pddstudio on 26/04/2017.
 */

@EFragment(R.layout.fragment_theme_patcher)
public class ThemePatcherFragment extends Fragment implements TemplateItemView.OpenColorPickerCallback {

	public static final String TAG = ThemePatcherFragment.class.getSimpleName();

	public static ThemePatcherFragment newInstance(ApkInfo apkInfo) {
		return ThemePatcherFragment_.builder().apkInfo(apkInfo).build();
	}

	@FragmentArg
	protected ApkInfo apkInfo;

	@Bean
	protected EventBusBean eventBus;

	@Bean
	protected ManifestProcessorBean manifestProcessor;

	@ViewById(R.id.theme_name_input)
	protected EditText themeNameEditText;

	@ViewById(R.id.theme_patcher_linear_layout)
	protected LinearLayout themePatcherLayout;

	private List<TemplateConfiguration> templates;
	private MaterialDialog preparationDialog;

	@Subscribe
	public void onColorSelectionCompleted(ColorChooserDialogEvent event) {
		if (event != null && !event.isOpenRequest()) {
			String resultColor = event.getResultColor();
			updateTemplate(event.getTemplateId(), event.getTemplateKey(), resultColor);
			Log.d(TAG, "Selected Color: " + resultColor + " id: " + event.getTemplateId());
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onPatchingPreparationResultReceived(PatchThemePreparationEvent event) {
		togglePreparationDialog(false);
		if (event != null && event.isSuccess()) {
			this.templates = event.getTemplateConfigurations();
			onPreparationSucceeded();
		} else {
			onPreparationFailed();
		}
	}

	@AfterViews
	protected void prepareManifest() {
		togglePreparationDialog(true);
		PatchTemplateService.prepareTargetTheme(getContext(), apkInfo);
	}

	@Override
	public void onStart() {
		super.onStart();
		eventBus.register(this);
	}

	@Override
	public void onStop() {
		eventBus.unregister(this);
		super.onStop();
	}

	@Click(R.id.build_patched_theme_btn)
	protected void onBuildPatchedTheme() {
		eventBus.post(new PatchThemeDialogEvent(true));
		PatchTemplateService.patchTargetTheme(getContext(), apkInfo, templates);
	}

	private void onPreparationFailed() {
		//TODO: show error dialog and quit activity when pressing "okay"
		getActivity().finish();
	}

	private void onPreparationSucceeded() {
		//TODO: create TemplateItemView for each TemplateConfig and add it to this layout
		StreamSupport.stream(templates).peek(templateConfiguration -> Log.d(TAG, "Template: " + templateConfiguration.getTemplateId())).forEach(templateConfiguration -> {
			TemplateItemView templateItemView = new TemplateItemView(getContext());
			templateItemView.setTemplateConfiguration(templateConfiguration, ThemePatcherFragment.this);
			templateItemView.setTitle(templateConfiguration.getTemplateTitle());
			templateItemView.setDescription(templateConfiguration.getTemplateDescription());
			themePatcherLayout.addView(templateItemView);
		});
	}

	private void updateTemplate(String templateId, String templateKey, String templateValue) {
		StreamSupport.stream(templates).filter(templateConfiguration -> templateConfiguration.getTemplateId().equals(templateId)).forEach(templateConfiguration -> {
			HashMap<String, String> map = templateConfiguration.getThemeMappings();
			map.put(templateKey, templateValue);
			templateConfiguration.updateThemeMappings(map);
		});
		themePatcherLayout.removeAllViews();
		onPreparationSucceeded();
	}

	@Override
	public void openColorPicker(TemplateConfiguration templateConfiguration, String key, String value) {
		ColorChooserDialogEvent event = new ColorChooserDialogEvent(true, templateConfiguration.getTemplateId()).withTemplateKey(key);
		eventBus.post(event);
	}

	private void togglePreparationDialog(boolean showDialog) {
		if (showDialog) {
			preparationDialog = new MaterialDialog.Builder(getContext()).title(R.string.dialog_preparing_title)
															.content(R.string.dialog_preparing_content)
															.progress(true, -1)
															.cancelable(false)
															.canceledOnTouchOutside(false)
															.autoDismiss(false)
															.show();
		} else {
			if (preparationDialog != null && preparationDialog.isShowing()) {
				preparationDialog.dismiss();
			}
			preparationDialog = null;
		}
	}

}
{'fraction_non_alphanumeric': 0.09918283963227784, 'fraction_numerical': 0.0022471910112359553, 'mean_word_length': 6.08465991316932, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3318374', 'n_tokens_mistral': 3205, 'n_tokens_neox': 2879, 'n_words': 491}