Skip to content

Commit

Permalink
feat: support change RepeatIcon DPI by number for RepeaterPlus
Browse files Browse the repository at this point in the history
  • Loading branch information
Hicores committed May 19, 2022
1 parent 9925a21 commit fbebc6e
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 63 deletions.
100 changes: 37 additions & 63 deletions app/src/main/java/com/hicore/dialog/RepeaterPlusIconSettingDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
Expand All @@ -38,12 +41,14 @@
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.core.view.ViewCompat;
import cc.ioctl.util.ui.FaultyDialog;
import cc.ioctl.util.ui.drawable.DebugDrawable;
import de.robv.android.xposed.XposedBridge;
import io.github.qauxv.R;
import io.github.qauxv.config.ConfigManager;
import io.github.qauxv.ui.CustomDialog;
Expand All @@ -58,7 +63,7 @@
import java.io.InputStream;

public class RepeaterPlusIconSettingDialog implements View.OnClickListener,
DialogInterface.OnClickListener, RadioGroup.OnCheckedChangeListener,
DialogInterface.OnClickListener, TextWatcher,
CompoundButton.OnCheckedChangeListener {

public static final String qn_repeat_icon_data = "qn_repeat_plus_icon_data";
Expand All @@ -73,9 +78,9 @@ public class RepeaterPlusIconSettingDialog implements View.OnClickListener,
private final EditText pathInput;
private final ImageView prevImgView;
private final CheckBox specDpi;
private final RadioGroup dpiGroup;
private final LinearLayout linearLayoutDpi;
private final TextView textViewWarning;
private final EditText InputDPI;
private byte[] targetIconData;
@Nullable
private String targetIconPathHint;
Expand All @@ -91,7 +96,7 @@ public RepeaterPlusIconSettingDialog(Context context) {
ctx = dialog.getContext();
dialog.setCanceledOnTouchOutside(false);
@SuppressLint("InflateParams") View v = LayoutInflater.from(ctx)
.inflate(R.layout.select_repeater_icon_dialog, null);
.inflate(R.layout.select_repeater_icon_dialog_plus, null);
loadBtn = v.findViewById(R.id.selectRepeaterIcon_buttonLoadFile);
loadBtn.setOnClickListener(this);
browseBtn = v.findViewById(R.id.selectRepeaterIcon_buttonBrowseImg);
Expand All @@ -103,11 +108,13 @@ public RepeaterPlusIconSettingDialog(Context context) {
ViewCompat.setBackground(prevImgView, new DebugDrawable(ctx));
specDpi = v.findViewById(R.id.selectRepeaterIcon_checkBoxSpecifyDpi);
specDpi.setOnCheckedChangeListener(this);
dpiGroup = v.findViewById(R.id.selectRepeaterIcon_RadioGroupDpiList);
dpiGroup.setOnCheckedChangeListener(this);

pathInput = v.findViewById(R.id.selectRepeaterIcon_editTextIconLocation);
linearLayoutDpi = v.findViewById(R.id.selectRepeaterIcon_linearLayoutDpi);
textViewWarning = v.findViewById(R.id.selectRepeaterIcon_textViewWarnMsg);
InputDPI = v.findViewById(R.id.selectRepeaterIcon_InputDpi);
InputDPI.addTextChangedListener(this);
InputDPI.setText(String.valueOf(getDpiSet()));
dialog.setView(v);
}

Expand Down Expand Up @@ -148,25 +155,6 @@ public static int getDpiSet(){
return cfg.getIntOrDefault(qn_repeat_icon_dpi, 50);
}

private static int getSelectedIdByDpi(int dpi) {
switch (dpi) {
case 640:
return R.id.selectRepeaterIcon_RadioButtonXxxhdpi;
case 480:
return R.id.selectRepeaterIcon_RadioButtonXxhdpi;
case 320:
return R.id.selectRepeaterIcon_RadioButtonXhdpi;
case 240:
return R.id.selectRepeaterIcon_RadioButtonHdpi;
case 160:
return R.id.selectRepeaterIcon_RadioButtonMdpi;
case 120:
return R.id.selectRepeaterIcon_RadioButtonLdpi;
default:
return 0;
}
}

public static void createAndShowDialog(Context ctx) {
new RepeaterPlusIconSettingDialog(ctx).show();
}
Expand Down Expand Up @@ -198,39 +186,16 @@ public AlertDialog show() {
linearLayoutDpi.setVisibility(View.GONE);
} else {
linearLayoutDpi.setVisibility(View.VISIBLE);
int id = getSelectedIdByDpi(dpi);
if (id == 0) {
specDpi.setChecked(false);
dpiGroup.setVisibility(View.GONE);
} else {
dpiGroup.check(id);
}
}
currentIconDrawable = new BitmapDrawable(ctx.getResources(), currentIcon);
prevImgView.setImageDrawable(currentIconDrawable);
return dialog;
}

private int getCurrentSelectedDpi() {
if (!specDpi.isChecked()) {
return 0;
}
int id = dpiGroup.getCheckedRadioButtonId();
switch (id) {
case R.id.selectRepeaterIcon_RadioButtonXxxhdpi:
return 640;
case R.id.selectRepeaterIcon_RadioButtonXxhdpi:
return 480;
case R.id.selectRepeaterIcon_RadioButtonXhdpi:
return 320;
case R.id.selectRepeaterIcon_RadioButtonHdpi:
return 240;
case R.id.selectRepeaterIcon_RadioButtonMdpi:
return 160;
case R.id.selectRepeaterIcon_RadioButtonLdpi:
return 120;
default:
return 0;
private int getCurrentDPI(){
try{
return Integer.parseInt(InputDPI.getText().toString());
}catch (Exception e){
return 45;
}
}

Expand Down Expand Up @@ -276,7 +241,7 @@ public void onClick(View v) {
prevImgView.setImageDrawable(ResUtils.loadDrawableFromAsset("repeat.png", ctx));
} else if (v == saveBtn) {
if (targetIconData != null) {
int dpi = getCurrentSelectedDpi();
int dpi = getCurrentDPI();
ConfigManager cfg = ConfigManager.getDefaultConfig();
cfg.putBytes(qn_repeat_icon_data, targetIconData);
cfg.putInt(qn_repeat_icon_dpi, dpi);
Expand All @@ -295,7 +260,7 @@ public void onClick(View v) {
sCachedRepeaterIcon = null;
} else {
ConfigManager cfg = ConfigManager.getDefaultConfig();
cfg.putInt(qn_repeat_icon_dpi, getCurrentSelectedDpi());
cfg.putInt(qn_repeat_icon_dpi, getCurrentDPI());
cfg.save();
dialog.dismiss();
sCachedRepeaterIcon = null;
Expand Down Expand Up @@ -338,17 +303,17 @@ private void updateBitmapPreview(@NonNull byte[] data) {
}
currentIcon = bm;
targetIconData = data;
currentIcon.setDensity(getCurrentSelectedDpi());
currentIcon.setDensity(getCurrentDPI());
currentIconDrawable = new BitmapDrawable(ctx.getResources(), currentIcon);
prevImgView.setImageDrawable(currentIconDrawable);
useDefault = false;
linearLayoutDpi.setVisibility(View.VISIBLE);
}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (currentIcon != null) {
currentIcon.setDensity(getCurrentSelectedDpi());
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView == specDpi) {
InputDPI.setVisibility(isChecked ? View.VISIBLE : View.GONE);
currentIcon.setDensity(getCurrentDPI());
if (currentIconDrawable != null) {
currentIconDrawable = new BitmapDrawable(ctx.getResources(), currentIcon);
prevImgView.setImageDrawable(currentIconDrawable);
Expand All @@ -357,10 +322,19 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView == specDpi) {
dpiGroup.setVisibility(isChecked ? View.VISIBLE : View.GONE);
currentIcon.setDensity(getCurrentSelectedDpi());
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
if (currentIcon != null) {
currentIcon.setDensity(getCurrentDPI());
if (currentIconDrawable != null) {
currentIconDrawable = new BitmapDrawable(ctx.getResources(), currentIcon);
prevImgView.setImageDrawable(currentIconDrawable);
Expand Down
107 changes: 107 additions & 0 deletions app/src/main/res/layout/select_repeater_icon_dialog_plus.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="请输入图片文件路径(建议使用支持半透明的PNG格式图片)"
tools:ignore="RtlHardcoded" />

<EditText
android:id="@+id/selectRepeaterIcon_editTextIconLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="图片路径..."
android:inputType="text" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">

<Button
android:id="@+id/selectRepeaterIcon_buttonLoadFile"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="加载"
tools:ignore="RtlHardcoded" />

<Button
android:id="@+id/selectRepeaterIcon_buttonBrowseImg"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="浏览..."
tools:ignore="RtlHardcoded" />
</RelativeLayout>

<LinearLayout
android:id="@+id/selectRepeaterIcon_linearLayoutDpi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<CheckBox
android:id="@+id/selectRepeaterIcon_checkBoxSpecifyDpi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:checked="true"
android:text="图片DPI" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/selectRepeaterIcon_InputDpi"
android:layout_marginTop="5dp"/>
</LinearLayout>

<TextView
android:id="@+id/selectRepeaterIcon_textViewWarnMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF2E2E"
android:visibility="gone" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">

<ImageView
android:id="@+id/selectRepeaterIcon_imageViewPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_toEndOf="@id/selectRepeaterIcon_tmp_textViewPreviewText"
android:layout_toRightOf="@id/selectRepeaterIcon_tmp_textViewPreviewText" />

<TextView
android:id="@+id/selectRepeaterIcon_tmp_textViewPreviewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:text="预览:" />

<Button
android:id="@+id/selectRepeaterIcon_buttonRestoreDefaultIcon"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="恢复默认图片" />
</RelativeLayout>
</LinearLayout>

0 comments on commit fbebc6e

Please sign in to comment.