Skip to content

Commit

Permalink
Change how to get internal resources
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduytran0 committed Aug 11, 2020
1 parent a25089f commit 4726a8d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
17 changes: 10 additions & 7 deletions app/src/main/java/com/kdt/handleview/ActionPopupWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@
import android.support.v7.app.*;

public class ActionPopupWindow extends PinnedPopupWindow implements OnClickListener {
private static final int POPUP_TEXT_LAYOUT = getInternalId("layout", "text_edit_action_popup_text");
private final int POPUP_TEXT_LAYOUT;
private TextView mEditTextView;
private TextView mDeleteTextView;

public ActionPopupWindow(HandleView handleView) {
super(handleView);
POPUP_TEXT_LAYOUT = Tools.getInternalResId(handleView.getContext(), "layout", "text_edit_action_popup_text");
}

/*
private static int getInternalId(String type, String name) {
try {
for (Class perType : Class.forName("com.android.internal.R").getDeclaredClasses()) {
if (perType.getSimpleName().equals(type)) {
Expand Down Expand Up @@ -72,10 +75,10 @@ private static int getInternalId(String type, String name) {
return -1;
}
*/
@Override
protected void createPopupWindow() {
mPopupWindow = new PopupWindow(mHandleView.getContext(), null, getInternalId("attr", "textSelectHandleWindowStyle"));
mPopupWindow = new PopupWindow(mHandleView.getContext(), null, Tools.getInternalResId(mHandleView.getContext(), "attr", "textSelectHandleWindowStyle"));
mPopupWindow.setClippingEnabled(true);
}

Expand All @@ -84,7 +87,7 @@ protected void initContentView() {
LinearLayout linearLayout = new LinearLayout(mHandleView.getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
mContentView = linearLayout;
mContentView.setBackgroundResource(getInternalId("drawable", "text_edit_paste_window"));
mContentView.setBackgroundResource(Tools.getInternalResId(mHandleView.getContext(), "drawable", "text_edit_paste_window"));

LayoutInflater inflater = (LayoutInflater) mHandleView.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Expand All @@ -101,7 +104,7 @@ protected void initContentView() {
mDeleteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null);
mDeleteTextView.setLayoutParams(wrapContent);
mContentView.addView(mDeleteTextView);
mDeleteTextView.setText(getInternalId("string", "delete"));
mDeleteTextView.setText(Tools.getInternalResId(mHandleView.getContext(), "string", "delete"));
mDeleteTextView.setOnClickListener(this);
}

Expand Down Expand Up @@ -188,7 +191,7 @@ public void onClick(View view2) {

dialog.show();
} else if (view == mDeleteTextView) {
alert.setMessage(view.getResources().getString(getInternalId("string", "delete")) + " " + mHandleView.mView.getText() + "?");
alert.setMessage(view.getResources().getString(Tools.getInternalResId(mHandleView.getContext(), "string", "delete")) + " " + mHandleView.mView.getText() + "?");
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){

@Override
Expand Down
23 changes: 2 additions & 21 deletions app/src/main/java/com/kdt/handleview/HandleView.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,6 @@ public PositionListener getPositionListener() {
}
return mPositionListener;
}

private int getInternalId(String type, String name) {
try {
for (Class perType : Class.forName("com.android.internal.R").getDeclaredClasses()) {
if (perType.getSimpleName().equals(type)) {
try {
Field f = perType.getDeclaredField(name);
f.setAccessible(true);
return (int) f.get(null);
} catch (Throwable th) {
th.printStackTrace();
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return -1;
}

public HandleView(ControlView view) {
super(view.getContext());
Expand All @@ -100,15 +81,15 @@ public HandleView(ControlView view) {
mDownWidth = view.getLayoutParams().width;
mDownHeight = view.getLayoutParams().height;

mContainer = new PopupWindow(view.getContext(), null, getInternalId("attr", "textSelectHandleWindowStyle"));
mContainer = new PopupWindow(view.getContext(), null, Tools.getInternalResId(getContext(), "attr", "textSelectHandleWindowStyle"));
mContainer.setSplitTouchEnabled(true);
mContainer.setClippingEnabled(false);
mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
mContainer.setContentView(this);

mDrawableLtr = view.getContext().getDrawable(R.drawable.text_select_handle_left_material);
mDrawableRtl = view.getContext().getDrawable(R.drawable.text_select_handle_right_material);
mMinSize = view.getContext().getResources().getDimensionPixelSize(getInternalId("dimen", "text_handle_min_size"));
mMinSize = view.getContext().getResources().getDimensionPixelSize(Tools.getInternalResId(getContext(), "dimen", "text_handle_min_size"));

setOnLongClickListener(this);

Expand Down
27 changes: 17 additions & 10 deletions app/src/main/java/net/kdt/pojavlaunch/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,36 @@ public static void extractAssetFolder(Activity ctx, String path, String output,
showError(ctx, e);
}
}

public static int getInternalResId(Context ctx, String type, String name) {
Resources res = ctx.getResources();
int internalId = res.getIdentifier(name, type, "com.android.internal");
// = Resources.getSystem().getIdentifier();

if (internalId == 0) {
internalId = res.getIdentifier(name, type, "android");
}

return internalId;
}

public static void showError(Context ctx, Throwable e)
{
public static void showError(Context ctx, Throwable e) {
showError(ctx, e, false);
}

public static void showError(final Context ctx, final Throwable e, final boolean exitIfOk)
{
public static void showError(final Context ctx, final Throwable e, final boolean exitIfOk) {
showError(ctx, R.string.global_error, e, exitIfOk, false);
}

public static void showError(final Context ctx, final int titleId, final Throwable e, final boolean exitIfOk)
{
public static void showError(final Context ctx, final int titleId, final Throwable e, final boolean exitIfOk) {
showError(ctx, titleId, e, exitIfOk, false);
}

private static void showError(final Context ctx, final int titleId, final Throwable e, final boolean exitIfOk, final boolean showMore)
{
private static void showError(final Context ctx, final int titleId, final Throwable e, final boolean exitIfOk, final boolean showMore) {
Runnable runnable = new Runnable(){

@Override
public void run()
{
public void run() {
final String errMsg = showMore ? Log.getStackTraceString(e): e.getMessage();
new AlertDialog.Builder((Context) ctx)
.setTitle(titleId)
Expand Down

0 comments on commit 4726a8d

Please sign in to comment.