这是indexloc提供的服务,不要输入任何密码
Skip to content

Commit bab9ef3

Browse files
Changed: Rename TermuxAPIActivity to TermuxAPIMainActivity stage 2
The `TermuxAPILauncherActivity` activity alias has been added for `TermuxAPIMainActivity`. This will allow disabling the launcher activity, but still allow the main activity to be launched from Termux app. The launcher activity will also now be allowed to be enabled again without having to reinstall the app with the `Enable Launcher Icon` button in the main activity if its currently disabled.
1 parent 280e5c7 commit bab9ef3

File tree

4 files changed

+124
-17
lines changed

4 files changed

+124
-17
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,20 @@
6767
tools:ignore="GoogleAppIndexingWarning">
6868

6969
<activity
70-
android:name=".activities.TermuxAPIActivity"
71-
android:theme="@style/Theme.BaseActivity.DayNight.NoActionBar"
72-
android:exported="true">
70+
android:name=".activities.TermuxAPIMainActivity"
71+
android:exported="true"
72+
android:theme="@style/Theme.BaseActivity.DayNight.NoActionBar">
73+
</activity>
74+
75+
<activity-alias
76+
android:name=".activities.TermuxAPILauncherActivity"
77+
android:exported="true"
78+
android:targetActivity=".activities.TermuxAPIMainActivity">
7379
<intent-filter>
7480
<action android:name="android.intent.action.MAIN"/>
7581
<category android:name="android.intent.category.LAUNCHER"/>
7682
</intent-filter>
77-
</activity>
83+
</activity-alias>
7884

7985
<activity android:name=".activities.TermuxApiPermissionActivity"
8086
android:theme="@android:style/Theme.NoDisplay"

app/src/main/java/com/termux/api/activities/TermuxAPIMainActivity.java

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,35 @@
1515
import com.termux.shared.activity.ActivityUtils;
1616
import com.termux.shared.activity.media.AppCompatActivityUtils;
1717
import com.termux.shared.android.AndroidUtils;
18+
import com.termux.shared.android.PackageUtils;
1819
import com.termux.shared.android.PermissionUtils;
1920
import com.termux.shared.data.IntentUtils;
2021
import com.termux.shared.file.FileUtils;
2122
import com.termux.shared.logger.Logger;
23+
import com.termux.shared.markdown.MarkdownUtils;
2224
import com.termux.shared.models.ReportInfo;
2325
import com.termux.shared.termux.TermuxConstants;
2426
import com.termux.shared.termux.TermuxUtils;
2527
import com.termux.shared.termux.theme.TermuxThemeUtils;
2628
import com.termux.shared.theme.NightMode;
2729
import com.termux.api.R;
2830

29-
public class TermuxAPIActivity extends AppCompatActivity {
31+
public class TermuxAPIMainActivity extends AppCompatActivity {
3032

3133
private TextView mBatteryOptimizationNotDisabledWarning;
3234
private TextView mDisplayOverOtherAppsPermissionNotGrantedWarning;
3335

3436
private Button mDisableBatteryOptimization;
3537
private Button mGrantDisplayOverOtherAppsPermission;
3638

37-
private static final String LOG_TAG = "TermuxAPIActivity";
39+
public static final String LOG_TAG = "TermuxAPIMainActivity";
3840

3941
@Override
4042
protected void onCreate(Bundle savedInstanceState) {
4143
Logger.logDebug(LOG_TAG, "onCreate");
4244

4345
super.onCreate(savedInstanceState);
44-
setContentView(R.layout.activity_termux_api);
46+
setContentView(R.layout.activity_termux_api_main);
4547

4648
// Set NightMode.APP_NIGHT_MODE
4749
TermuxThemeUtils.setAppNightMode(this);
@@ -60,22 +62,25 @@ protected void onCreate(Bundle savedInstanceState) {
6062
mDisableBatteryOptimization.setOnClickListener(v -> requestDisableBatteryOptimizations());
6163

6264
mDisplayOverOtherAppsPermissionNotGrantedWarning = findViewById(R.id.textview_display_over_other_apps_not_granted_warning);
63-
mGrantDisplayOverOtherAppsPermission = findViewById(R.id.btn_grant_display_over_other_apps_permission);
65+
mGrantDisplayOverOtherAppsPermission = findViewById(R.id.button_grant_display_over_other_apps_permission);
6466
mGrantDisplayOverOtherAppsPermission.setOnClickListener(v -> requestDisplayOverOtherAppsPermission());
6567
}
6668

6769
@Override
6870
protected void onResume() {
6971
super.onResume();
7072

73+
Logger.logVerbose(LOG_TAG, "onResume");
74+
7175
checkIfBatteryOptimizationNotDisabled();
7276
checkIfDisplayOverOtherAppsPermissionNotGranted();
77+
setChangeLauncherActivityStateViews();
7378
}
7479

7580
@Override
7681
public boolean onCreateOptionsMenu(Menu menu) {
7782
super.onCreateOptionsMenu(menu);
78-
getMenuInflater().inflate(R.menu.activity_termux_api, menu);
83+
getMenuInflater().inflate(R.menu.activity_termux_api_main, menu);
7984
return true;
8085
}
8186

@@ -101,18 +106,18 @@ public void run() {
101106
String title = "About";
102107

103108
StringBuilder aboutString = new StringBuilder();
104-
aboutString.append(TermuxUtils.getAppInfoMarkdownString(TermuxAPIActivity.this, TermuxUtils.AppInfoMode.TERMUX_AND_PLUGIN_PACKAGE));
105-
aboutString.append("\n\n").append(AndroidUtils.getDeviceInfoMarkdownString(TermuxAPIActivity.this));
106-
aboutString.append("\n\n").append(TermuxUtils.getImportantLinksMarkdownString(TermuxAPIActivity.this));
109+
aboutString.append(TermuxUtils.getAppInfoMarkdownString(TermuxAPIMainActivity.this, TermuxUtils.AppInfoMode.TERMUX_AND_PLUGIN_PACKAGE));
110+
aboutString.append("\n\n").append(AndroidUtils.getDeviceInfoMarkdownString(TermuxAPIMainActivity.this));
111+
aboutString.append("\n\n").append(TermuxUtils.getImportantLinksMarkdownString(TermuxAPIMainActivity.this));
107112

108113
ReportInfo reportInfo = new ReportInfo(title,
109-
TermuxConstants.TERMUX_APP.TERMUX_SETTINGS_ACTIVITY_NAME, title);
114+
TermuxConstants.TERMUX_API_APP.TERMUX_API_MAIN_ACTIVITY_NAME, title);
110115
reportInfo.setReportString(aboutString.toString());
111116
reportInfo.setReportSaveFileLabelAndPath(title,
112117
Environment.getExternalStorageDirectory() + "/" +
113118
FileUtils.sanitizeFileName(TermuxConstants.TERMUX_APP_NAME + "-" + title + ".log", true, true));
114119

115-
ReportActivity.startReportActivity(TermuxAPIActivity.this, reportInfo);
120+
ReportActivity.startReportActivity(TermuxAPIMainActivity.this, reportInfo);
116121
}
117122
}.start();
118123
}
@@ -159,6 +164,54 @@ private void requestDisplayOverOtherAppsPermission() {
159164

160165

161166

167+
private void setChangeLauncherActivityStateViews() {
168+
String packageName = TermuxConstants.TERMUX_API_PACKAGE_NAME;
169+
String className = TermuxConstants.TERMUX_API_APP.TERMUX_API_LAUNCHER_ACTIVITY_NAME;
170+
171+
TextView changeLauncherActivityStateTextView = findViewById(R.id.textview_change_launcher_activity_state_details);
172+
changeLauncherActivityStateTextView.setText(MarkdownUtils.getSpannedMarkdownText(this,
173+
getString(R.string.msg_change_launcher_activity_state_info, packageName, getClass().getName())));
174+
175+
Button changeLauncherActivityStateButton = findViewById(R.id.button_change_launcher_activity_state);
176+
String stateChangeMessage;
177+
boolean newState;
178+
179+
Boolean currentlyDisabled = PackageUtils.isComponentDisabled(this,
180+
packageName, className, false);
181+
if (currentlyDisabled == null) {
182+
Logger.logError(LOG_TAG, "Failed to check if \"" + packageName + "/" + className + "\" launcher activity is disabled");
183+
changeLauncherActivityStateButton.setEnabled(false);
184+
changeLauncherActivityStateButton.setAlpha(.5f);
185+
changeLauncherActivityStateButton.setText(com.termux.shared.R.string.action_disable_launcher_icon);
186+
changeLauncherActivityStateButton.setOnClickListener(null);
187+
return;
188+
}
189+
190+
changeLauncherActivityStateButton.setEnabled(true);
191+
changeLauncherActivityStateButton.setAlpha(1f);
192+
if (currentlyDisabled) {
193+
changeLauncherActivityStateButton.setText(com.termux.shared.R.string.action_enable_launcher_icon);
194+
stateChangeMessage = getString(com.termux.shared.R.string.msg_enabling_launcher_icon, TermuxConstants.TERMUX_API_APP_NAME);
195+
newState = true;
196+
} else {
197+
changeLauncherActivityStateButton.setText(com.termux.shared.R.string.action_disable_launcher_icon);
198+
stateChangeMessage = getString(com.termux.shared.R.string.msg_disabling_launcher_icon, TermuxConstants.TERMUX_API_APP_NAME);
199+
newState = false;
200+
}
201+
202+
changeLauncherActivityStateButton.setOnClickListener(v -> {
203+
Logger.logInfo(LOG_TAG, stateChangeMessage);
204+
String errmsg = PackageUtils.setComponentState(this,
205+
packageName, className, newState, stateChangeMessage, true);
206+
if (errmsg == null)
207+
setChangeLauncherActivityStateViews();
208+
else
209+
Logger.logError(LOG_TAG, errmsg);
210+
});
211+
}
212+
213+
214+
162215
@Override
163216
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
164217
super.onActivityResult(requestCode, resultCode, data);

app/src/main/res/layout/activity_termux_api_main.xml

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
android:paddingBottom="@dimen/activity_vertical_margin"
2323
android:paddingLeft="@dimen/activity_horizontal_margin"
2424
android:paddingRight="@dimen/activity_horizontal_margin"
25-
tools:context=".activities.TermuxAPIActivity">
25+
tools:context=".activities.TermuxAPIMainActivity">
2626

2727
<com.google.android.material.textview.MaterialTextView
2828
android:id="@+id/textview_plugin_info"
@@ -35,8 +35,11 @@
3535
android:textStyle="normal"
3636
android:textColor="?android:textColorPrimary"
3737
android:textColorLink="?android:textColorLink"
38+
android:textIsSelectable="true"
3839
android:autoLink="web"/>
3940

41+
42+
4043
<View style="@style/ViewDivider"/>
4144

4245
<com.google.android.material.textview.MaterialTextView
@@ -50,6 +53,7 @@
5053
android:textStyle="normal"
5154
android:textColor="@color/red_error"
5255
android:textColorLink="@color/red_error_link"
56+
android:textIsSelectable="true"
5357
android:autoLink="web"/>
5458

5559
<com.google.android.material.button.MaterialButton
@@ -65,6 +69,8 @@
6569
app:strokeColor="?android:textColorPrimary"
6670
app:strokeWidth="2dp"/>
6771

72+
73+
6874
<View style="@style/ViewDivider"/>
6975

7076
<com.google.android.material.textview.MaterialTextView
@@ -78,11 +84,12 @@
7884
android:textStyle="normal"
7985
android:textColor="@color/red_error"
8086
android:textColorLink="@color/red_error_link"
87+
android:textIsSelectable="true"
8188
android:autoLink="web"/>
8289

8390
<com.google.android.material.button.MaterialButton
8491
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
85-
android:id="@+id/btn_grant_display_over_other_apps_permission"
92+
android:id="@+id/button_grant_display_over_other_apps_permission"
8693
android:layout_width="wrap_content"
8794
android:layout_height="wrap_content"
8895
android:layout_gravity="end"
@@ -93,6 +100,35 @@
93100
android:textColor="?android:textColorPrimary"
94101
app:strokeColor="?android:textColorPrimary"
95102
app:strokeWidth="2dp"/>
103+
104+
105+
106+
<View style="@style/ViewDivider"/>
107+
108+
<com.google.android.material.textview.MaterialTextView
109+
android:id="@+id/textview_change_launcher_activity_state_details"
110+
android:layout_width="match_parent"
111+
android:layout_height="wrap_content"
112+
android:paddingBottom="@dimen/activity_vertical_margin"
113+
android:gravity="start|center_vertical"
114+
android:textSize="14sp"
115+
android:textStyle="normal"
116+
android:textColor="?android:textColorPrimary"
117+
android:textColorLink="?android:textColorLink"
118+
android:textIsSelectable="true"
119+
android:autoLink="web"/>
120+
121+
<com.google.android.material.button.MaterialButton
122+
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
123+
android:id="@+id/button_change_launcher_activity_state"
124+
android:layout_width="wrap_content"
125+
android:layout_height="wrap_content"
126+
android:layout_gravity="end"
127+
android:gravity="center"
128+
android:textSize="12sp"
129+
android:textColor="?android:textColorPrimary"
130+
app:strokeColor="?android:textColorPrimary"
131+
app:strokeWidth="2dp"/>
96132
</LinearLayout>
97133

98134
</ScrollView>

app/src/main/res/values/strings.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232

33-
<!-- TermuxAPIActivity -->
33+
<!-- TermuxAPIMainActivity -->
3434
<string name="msg_battery_optimization_not_disabled_warning">Android battery optimizations
3535
should be disabled for the &TERMUX_API_APP_NAME; app so that termux-api script can start
3636
it from the background if its failing to do so. Do not worry, this will not drain battery,
@@ -43,13 +43,25 @@
4343
Depending on vendor you may need to do things like enable AutoStart, disable DuraSpeed,
4444
enable `Display pop-up windows while running in the background` for the app.</string>
4545
<string name="action_disable_battery_optimizations">Disable Battery Optimizations</string>
46+
4647
<string name="msg_display_over_other_apps_permission_not_granted_warning">The display over other
4748
apps permission should be granted to &TERMUX_API_APP_NAME; app for starting foreground
4849
activities from background. Check https://developer.android.com/guide/components/activities/background-starts
4950
for more info.
5051
</string>
5152
<string name="action_grant_display_over_other_apps_permission">Grant Draw Over Apps Permission</string>
5253

54+
<string name="msg_change_launcher_activity_state_info">The &TERMUX_API_APP_NAME; app does not
55+
require a require launcher activity/icon to function. You can optionally disable the launcher
56+
activity if you want and enable it again from the main activity if required.
57+
\n\nThe launcher activity is an alias for the current main activity of the app which can
58+
still be opened after disabling the launcher activity. The main activity can be opened
59+
from `&TERMUX_APP_NAME; app settings` -> `&TERMUX_API_APP_NAME;` -> `Open App` if the
60+
option has been implemented in your installed &TERMUX_APP_NAME; app version. Otherwise,
61+
running the `am start "%1$s/%2$s"` command in the &TERMUX_APP_NAME; app should open it.
62+
\n\nNote that on some devices the APIs may not function properly if the launcher activity is
63+
disabled.</string>
64+
5365
<string name="action_already_granted">Already Granted</string>
5466
<string name="action_already_disabled">Already Disabled</string>
5567
<string name="action_info">Info</string>

0 commit comments

Comments
 (0)