Skip to content

Commit

Permalink
Add setup guidance & request permission action
Browse files Browse the repository at this point in the history
  • Loading branch information
RainbowC0 committed Sep 4, 2024
1 parent 5e9fb14 commit 5f2feff
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 131 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
defaultConfig {
applicationId "cn.rbc.termuc"
minSdkVersion 21
targetSdkVersion 29
targetSdkVersion 30
versionCode 3
versionName "0.1.2"
}
Expand All @@ -18,8 +18,8 @@ android {
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
lintOptions {
checkReleaseBuilds false
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/assets/init
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
echo 设置存储 Setup storage
termux-setup-storage
DATA=package:com.termux
echo -n 设置悬浮窗权限 Setup draw over apps \(请按回车 Press Enter\);read _
am start -a android.settings.action.MANAGE_OVERLAY_PERMISSION -d $DATA>/dev/null
echo -n 设置禁用电池优化 Setup ignore battery optimizations \(请按回车 Press Enter\);read _
am start -a android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS>/dev/null
echo -n 设置关联启动与后台弹出权限 Setup Startup \& Background pop-ups permissions \(请按回车 Press Enter\);read _
am start -a android.settings.APPLICATION_DETAILS_SETTINGS -d $DATA>/dev/null
echo 设置allow-external-app为\`true' Setup allow-external-app to \`true\'
fl=/data/data/com.termux/files/home/.termux/termux.properties
if [ -f $fl ];then
awk '/^#/{print;next }/^\s*allow-external-apps/{gsub(/allow-external-apps.*/,"allow-external-apps=true");found=1}{print $0}END{if(!found)print "allow-external-apps=true"}' $fl>$TMPDIR/a.tmp && mv $TMPDIR/a.tmp $fl
else
mkdir -p `dirname $fl`
echo 'allow-external-apps=true'>$fl
fi
echo 安装Clang Install Clang
pkg i clang -y
apt autoremove --purge
apt clean
echo ok
80 changes: 1 addition & 79 deletions app/src/main/java/cn/rbc/termuc/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Application extends android.app.Application
KEY_TEXTSIZE = "textsize",
KEY_SHOW_HIDDEN = "showhidden",
KEY_CHECKAPP = "checkapp",
KEY_INITAPP = "initapp",
KEY_CFLAGS = "cflags",
KEY_COMPLETION = "completion",
KEY_LSP_HOST = "lsphost",
Expand Down Expand Up @@ -50,83 +51,4 @@ private void initConfs() {
lsp_host = sp.getString(KEY_LSP_HOST, "127.0.0.1");
lsp_port = Integer.parseInt(sp.getString(KEY_LSP_PORT, "48455"));
}

public static void testApp(Activity ctx, boolean manually) {
PackageManager pm = ctx.getPackageManager();
try {
pm.getPackageInfo("com.termux", PackageManager.GET_GIDS);
if (manually)
HelperUtils.show(Toast.makeText(ctx, R.string.installed, Toast.LENGTH_SHORT));
} catch (PackageManager.NameNotFoundException e) {
Builder bd = new Builder(ctx);
bd.setTitle(R.string.install_app);
bd.setMessage(R.string.confirm_install);
bd.setNegativeButton(android.R.string.cancel, null);
Install oc = new Install(ctx);
bd.setPositiveButton(android.R.string.ok, oc);
if (!manually)
bd.setNeutralButton(R.string.no_remind, oc);
bd.create().show();
}
}

private static class Install
implements DialogInterface.OnClickListener, SimpleAdapter.ViewBinder {
Activity mApp;
Install(Activity app) {
mApp = app;
}
public boolean setViewValue(View v, Object o, String k) {
if (v instanceof ImageView && o instanceof Drawable) {
((ImageView)v).setImageDrawable((Drawable)o);
return true;
}
return false;
}
public void onClick(DialogInterface d, int p) {
Activity app = mApp;
if (p==DialogInterface.BUTTON_NEUTRAL) {
app.getPreferences(MODE_PRIVATE).edit().putBoolean(MainActivity.TESTAPP, false).commit();
return;
}
Uri uri = Uri.parse("market://details?id=com.termux");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
PackageManager pm = app.getPackageManager();
List<ResolveInfo> lst = pm.queryIntentActivities(it, 0);
List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> m;
final Intent[] its = new Intent[lst.size()+2];
int i = 0;
for (ResolveInfo ri:lst) {
m = new ArrayMap<>();
m.put("i", ri.loadIcon(pm));
m.put("n", ri.loadLabel(pm));
list.add(m);
it = new Intent(Intent.ACTION_VIEW, uri);
it.setPackage(ri.activityInfo.packageName);
its[i++] = it;
}
m = new ArrayMap<>();
m.put("i", null);
m.put("n", "Github Release website");
list.add(m);
its[i++] = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/termux/termux-app/releases"));
m = new ArrayMap<>();
m.put("i", null);
m.put("n", "F-Droid website");
list.add(m);
its[i] = new Intent(Intent.ACTION_VIEW, Uri.parse("https://f-droid.org/packages/com.termux"));
Builder bd = new Builder(app);
bd.setTitle(R.string.install_via);
bd.setNegativeButton(android.R.string.cancel, null);
SimpleAdapter sadp = new SimpleAdapter(app, list, R.layout.file_item, new String[]{"i", "n"}, new int[]{R.id.file_icon, R.id.file_name});
sadp.setViewBinder(this);
bd.setAdapter(sadp, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int p) {
mApp.startActivity(its[p]);
}
});
bd.create().show();
}
}
}
19 changes: 8 additions & 11 deletions app/src/main/java/cn/rbc/termuc/EditFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import android.util.TypedValue;

public class EditFragment extends Fragment
implements OnTextChangeListener, DialogInterface.OnClickListener, Formatter, Runnable
implements OnTextChangeListener, DialogInterface.OnClickListener, Formatter
{
public final static int TYPE_C = 1;
public final static int TYPE_CPP = 2;
Expand Down Expand Up @@ -119,7 +119,7 @@ public CharSequence format(CharSequence txt, int width) {
}

private int mVer;
private long mSendTime;
//private long mSendTime;

public void onChanged(CharSequence c, int start, int ver, boolean ins, boolean typ) {
TextEditor editor = ed;
Expand Down Expand Up @@ -151,29 +151,26 @@ public void onChanged(CharSequence c, int start, int ver, boolean ins, boolean t
}
range.msg = (String)c;
changes.add(range);
//lastStr = (String)c;
//if (lastVer != ver) {
//HelperUtils.show(Toast.makeText(getContext()), text.isBatchEdit(),
//if (text.isBatchEdit())
Lsp lsp = MainActivity.lsp;
lsp.didChange(fl, ver, changes);
//lastStart = s;
//lastVer = ver;
// when inserting text and typing, call for completion
if (ins && typ && c.length()==1)
lsp.completionTry(fl, range.enl, range.enc+1, c.charAt(0));
changes.clear();
mVer = ver;
mSendTime = System.currentTimeMillis();
editor.postDelayed(this, 1000L);
//}
//mSendTime = System.currentTimeMillis();
//editor.postDelayed(this, 1000L);
}

/*
public void run() {
Lsp lsp = MainActivity.lsp;
if (lsp.lastReceivedTime()<mSendTime) {
lsp.didChange(fl, mVer, ed.getText().toString());
}
}

*/
@Override
public void onResume() {
super.onResume();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/cn/rbc/termuc/Lsp.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Lsp implements Runnable {
private Handler mRead;

public void start(final Context mC, Handler read) {
Utils.run(mC, "/system/bin/toybox", new String[]{"nc", "-l", "-s", Application.lsp_host, "-p", Integer.toString(Application.lsp_port), "clangd", "--header-insertion-decorators=0", "--completion-style=bundled"}, Environment.getExternalStorageDirectory().getAbsolutePath(), true);
Utils.run(mC, "/system/bin/toybox", new String[]{"nc", "-l", "-s", Application.lsp_host, "-p", Integer.toString(Application.lsp_port), "clangd", "--header-insertion-decorators=0"}, Utils.ROOT.getAbsolutePath(), true);
mExecutor = Executors.newSingleThreadExecutor();
sk = new Socket();
mRead = read;
Expand Down
39 changes: 27 additions & 12 deletions app/src/main/java/cn/rbc/termuc/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
import android.app.AlertDialog.Builder;
import android.util.ArrayMap;
import android.graphics.drawable.*;
import android.provider.*;

public class MainActivity extends Activity implements
ActionBar.OnNavigationListener, OnGlobalLayoutListener,
AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener,
DialogInterface.OnClickListener, MenuItem.OnMenuItemClickListener, Runnable {

public final static int SETTING = 0;
public final static String PWD = "p", SHOWLIST = "l", FILES = "o", TESTAPP = "t";
public final static int SETTING = 0, ACCESS_FILE = 1;
public final static String PWD = "p", SHOWLIST = "l", FILES = "o", TESTAPP = "t", INITAPP = "i";
private ArrayAdapter<String> hda;
private FileAdapter adp;
private EditFragment lastFrag = null;
Expand Down Expand Up @@ -106,26 +107,32 @@ protected void onCreate(Bundle savedInstanceState) {
l.setOnItemClickListener(this);
l.setOnItemLongClickListener(this);
mSearchAction = new SearchAction(this);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk >= android.os.Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
Intent it = new Intent();
it.setAction(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
it.setData(Uri.parse("package:"+getPackageName()));
startActivityForResult(it, ACCESS_FILE);
} else if (sdk >= android.os.Build.VERSION_CODES.M)
requestPermissions(new String[]{
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.MANAGE_EXTERNAL_STORAGE
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
}, PackageManager.PERMISSION_GRANTED);
}
if (!pref.getBoolean(TESTAPP, true))
return;
Application.testApp(this, false);
if (pref.getBoolean(TESTAPP, true))
Utils.testApp(this, false);
if (pref.getBoolean(INITAPP, true))
Utils.initBack(this, false);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PackageManager.PERMISSION_GRANTED
&& grantResults[0] == requestCode
&& grantResults[1] == requestCode
&& grantResults[2] == requestCode)
&& grantResults[1] == requestCode)
refresh();
else
toast(getText(R.string.request_failed));
}

private void refresh() {
Expand Down Expand Up @@ -412,7 +419,8 @@ protected void onResume() {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SETTING) {
switch (requestCode) {
case SETTING:
if (resultCode == RESULT_OK) {
FragmentManager fm = getFragmentManager();
for (int i=getActionBar().getNavigationItemCount() - 1;i >= 0;i--) {
Expand All @@ -425,6 +433,13 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
} else if (resultCode == RESULT_FIRST_USER) {
recreate();
}
break;
case ACCESS_FILE:
if (resultCode == RESULT_OK)
refresh();
else
toast(getText(R.string.request_failed));
break;
}
}

Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/cn/rbc/termuc/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected void onCreate(Bundle savedInstanceState) {
mHost = (EditTextPreference)findPreference(Application.KEY_LSP_HOST);
mPort = (EditTextPreference)findPreference(Application.KEY_LSP_PORT);
findPreference(Application.KEY_CHECKAPP).setOnPreferenceClickListener(this);
findPreference(Application.KEY_INITAPP).setOnPreferenceClickListener(this);

mDark = Application.dark_mode;
mWrap = Application.wordwrap;
Expand All @@ -55,7 +56,10 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public boolean onPreferenceClick(Preference p1) {
Application.testApp(this, true);
if (Application.KEY_CHECKAPP.equals(p1.getKey()))
Utils.testApp(this, true);
else
Utils.initBack(this, true);
return true;
}

Expand Down
Loading

0 comments on commit 5f2feff

Please sign in to comment.