mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-09 12:04:03 +08:00
Create termux-shared library package for all termux constants and shared utils
The termux plugins should use this library instead of hardcoding "com.termux" values in their source code. The library can be included as a dependency by plugins and third party apps by including the following line in the build.gradle where x.xxx is the version number, once its published. `implementation 'com.termux:termux-shared:x.xxx'` The `TermuxConstants` class has been updated to `v0.17.0`, `TermuxPreferenceConstants` to `v0.9.0` and `TermuxPropertyConstants` to `v0.6.0`. Check their Changelog sections for info on changes. Some typos and redundant code has also been fixed.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package com.termux.shared.interact;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.text.Selection;
|
||||
import android.util.TypedValue;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
public final class DialogUtils {
|
||||
|
||||
public interface TextSetListener {
|
||||
void onTextSet(String text);
|
||||
}
|
||||
|
||||
public static void textInput(Activity activity, int titleText, String initialText,
|
||||
int positiveButtonText, final TextSetListener onPositive,
|
||||
int neutralButtonText, final TextSetListener onNeutral,
|
||||
int negativeButtonText, final TextSetListener onNegative,
|
||||
final DialogInterface.OnDismissListener onDismiss) {
|
||||
final EditText input = new EditText(activity);
|
||||
input.setSingleLine();
|
||||
if (initialText != null) {
|
||||
input.setText(initialText);
|
||||
Selection.setSelection(input.getText(), initialText.length());
|
||||
}
|
||||
|
||||
final AlertDialog[] dialogHolder = new AlertDialog[1];
|
||||
input.setImeActionLabel(activity.getResources().getString(positiveButtonText), KeyEvent.KEYCODE_ENTER);
|
||||
input.setOnEditorActionListener((v, actionId, event) -> {
|
||||
onPositive.onTextSet(input.getText().toString());
|
||||
dialogHolder[0].dismiss();
|
||||
return true;
|
||||
});
|
||||
|
||||
float dipInPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, activity.getResources().getDisplayMetrics());
|
||||
// https://www.google.com/design/spec/components/dialogs.html#dialogs-specs
|
||||
int paddingTopAndSides = Math.round(16 * dipInPixels);
|
||||
int paddingBottom = Math.round(24 * dipInPixels);
|
||||
|
||||
LinearLayout layout = new LinearLayout(activity);
|
||||
layout.setOrientation(LinearLayout.VERTICAL);
|
||||
layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
|
||||
layout.setPadding(paddingTopAndSides, paddingTopAndSides, paddingTopAndSides, paddingBottom);
|
||||
layout.addView(input);
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity)
|
||||
.setTitle(titleText).setView(layout)
|
||||
.setPositiveButton(positiveButtonText, (d, whichButton) -> onPositive.onTextSet(input.getText().toString()));
|
||||
|
||||
if (onNeutral != null) {
|
||||
builder.setNeutralButton(neutralButtonText, (dialog, which) -> onNeutral.onTextSet(input.getText().toString()));
|
||||
}
|
||||
|
||||
if (onNegative == null) {
|
||||
builder.setNegativeButton(android.R.string.cancel, null);
|
||||
} else {
|
||||
builder.setNegativeButton(negativeButtonText, (dialog, which) -> onNegative.onTextSet(input.getText().toString()));
|
||||
}
|
||||
|
||||
if (onDismiss != null) builder.setOnDismissListener(onDismiss);
|
||||
|
||||
dialogHolder[0] = builder.create();
|
||||
dialogHolder[0].setCanceledOnTouchOutside(false);
|
||||
dialogHolder[0].show();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,71 @@
|
||||
package com.termux.shared.interact;
|
||||
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.termux.shared.R;
|
||||
import com.termux.shared.data.DataUtils;
|
||||
import com.termux.shared.logger.Logger;
|
||||
|
||||
public class ShareUtils {
|
||||
|
||||
/**
|
||||
* Open the system app chooser that allows the user to select which app to send the intent.
|
||||
*
|
||||
* @param context The context for operations.
|
||||
* @param intent The intent that describes the choices that should be shown.
|
||||
* @param title The title for choose menu.
|
||||
*/
|
||||
private static void openSystemAppChooser(final Context context, final Intent intent, final String title) {
|
||||
if(context == null) return;
|
||||
|
||||
final Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
|
||||
chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
|
||||
chooserIntent.putExtra(Intent.EXTRA_TITLE, title);
|
||||
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(chooserIntent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Share text.
|
||||
*
|
||||
* @param context The context for operations.
|
||||
* @param subject The subject for sharing.
|
||||
* @param text The text to share.
|
||||
*/
|
||||
public static void shareText(final Context context, final String subject, final String text) {
|
||||
if(context == null) return;
|
||||
|
||||
final Intent shareTextIntent = new Intent(Intent.ACTION_SEND);
|
||||
shareTextIntent.setType("text/plain");
|
||||
shareTextIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
|
||||
shareTextIntent.putExtra(Intent.EXTRA_TEXT, DataUtils.getTruncatedCommandOutput(text, DataUtils.TRANSACTION_SIZE_LIMIT_IN_BYTES, false, false, false));
|
||||
|
||||
openSystemAppChooser(context, shareTextIntent, context.getString(R.string.title_share_with));
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the text to clipboard.
|
||||
*
|
||||
* @param context The context for operations.
|
||||
* @param text The text to copy.
|
||||
* @param toastString If this is not {@code null} or empty, then a toast is shown if copying to
|
||||
* clipboard is successful.
|
||||
*/
|
||||
public static void copyTextToClipboard(final Context context, final String text, final String toastString) {
|
||||
if(context == null) return;
|
||||
|
||||
final ClipboardManager clipboardManager = ContextCompat.getSystemService(context, ClipboardManager.class);
|
||||
|
||||
if (clipboardManager != null) {
|
||||
clipboardManager.setPrimaryClip(ClipData.newPlainText(null, text));
|
||||
if (toastString != null && !toastString.isEmpty())
|
||||
Logger.showToast(context, toastString, true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user