Skip to content

Commit d4cfdb2

Browse files
Changed: Rename TermuxWidgetActivity to TermuxWidgetMainActivity stage 2
The `TermuxWidgetLauncherActivity` activity alias has been added for `TermuxWidgetMainActivity`. This will allow disabling the launcher activity, but still allow the main activity to be launched from Termux app or from widget title in future. 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 639483a commit d4cfdb2

File tree

4 files changed

+109
-47
lines changed

4 files changed

+109
-47
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
android:label="@string/app_name"
1616
android:theme="@android:style/Theme.Material.Light.DarkActionBar"
1717
tools:ignore="GoogleAppIndexingWarning">
18+
1819
<receiver
1920
android:name=".TermuxWidgetProvider"
2021
android:exported="false"
@@ -30,14 +31,20 @@
3031
<receiver android:name=".TermuxWidgetControlExecutorReceiver" />
3132

3233
<activity
33-
android:name=".activities.TermuxWidgetActivity"
34-
android:theme="@style/Theme.MaterialComponents.DayNight.TermuxPrimaryActivity" >
34+
android:name=".activities.TermuxWidgetMainActivity"
3535
android:exported="true"
36+
android:theme="@style/Theme.MaterialComponents.DayNight.TermuxPrimaryActivity">
37+
</activity>
38+
39+
<activity-alias
40+
android:name=".activities.TermuxWidgetLauncherActivity"
41+
android:exported="true"
42+
android:targetActivity=".activities.TermuxWidgetMainActivity">
3643
<intent-filter>
3744
<action android:name="android.intent.action.MAIN"/>
3845
<category android:name="android.intent.category.LAUNCHER"/>
3946
</intent-filter>
40-
</activity>
47+
</activity-alias>
4148

4249
<activity
4350
android:name=".TermuxCreateShortcutActivity"

app/src/main/java/com/termux/widget/activities/TermuxWidgetMainActivity.java

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.termux.shared.file.FileUtils;
1818
import com.termux.shared.file.TermuxFileUtils;
1919
import com.termux.shared.logger.Logger;
20+
import com.termux.shared.markdown.MarkdownUtils;
2021
import com.termux.shared.models.errors.Error;
2122
import com.termux.shared.packages.PackageUtils;
2223
import com.termux.shared.termux.TermuxConstants;
@@ -30,9 +31,9 @@
3031
import java.util.List;
3132
import java.util.regex.Pattern;
3233

33-
public class TermuxWidgetActivity extends AppCompatActivity {
34+
public class TermuxWidgetMainActivity extends AppCompatActivity {
3435

35-
private static final String LOG_TAG = "TermuxWidgetActivity";
36+
public static final String LOG_TAG = "TermuxWidgetMainActivity";
3637

3738
/** Termux:Widget app data home directory path. */
3839
public static final String TERMUX_WIDGET_DATA_HOME_DIR_PATH = TermuxConstants.TERMUX_DATA_HOME_DIR_PATH + "/widget"; // Default: "/data/data/com.termux/files/home/.termux/widget"
@@ -48,13 +49,12 @@ protected void onCreate(Bundle savedInstanceState) {
4849

4950
Logger.logDebug(LOG_TAG, "onCreate");
5051

51-
setContentView(R.layout.activity_termux_widget);
52+
setContentView(R.layout.activity_termux_widget_main);
5253

5354
TextView pluginInfo = findViewById(R.id.textview_plugin_info);
5455
pluginInfo.setText(getString(R.string.plugin_info, TermuxConstants.TERMUX_GITHUB_REPO_URL,
5556
TermuxConstants.TERMUX_WIDGET_GITHUB_REPO_URL));
5657

57-
setDisableLauncherIconViews();
5858
setDynamicShortcutsViews();
5959
setRefreshAllWidgetsViews();
6060
}
@@ -65,6 +65,7 @@ protected void onResume() {
6565

6666
Logger.logVerbose(LOG_TAG, "onResume");
6767

68+
setChangeLauncherActivityStateViews();
6869
setMaxShortcutsLimitView();
6970
}
7071

@@ -82,14 +83,49 @@ private void setMaxShortcutsLimitView() {
8283
}
8384
}
8485

85-
private void setDisableLauncherIconViews() {
86-
Button disableLauncherIconButton = findViewById(R.id.btn_disable_launcher_icon);
87-
disableLauncherIconButton.setOnClickListener(v -> {
88-
String message = getString(R.string.msg_disabling_launcher_icon, TermuxConstants.TERMUX_WIDGET_APP_NAME);
89-
Logger.logInfo(LOG_TAG, message);
90-
PackageUtils.setComponentState(TermuxWidgetActivity.this,
91-
TermuxConstants.TERMUX_WIDGET_PACKAGE_NAME, TermuxConstants.TERMUX_WIDGET.TERMUX_WIDGET_ACTIVITY_NAME,
92-
false, message, true);
86+
private void setChangeLauncherActivityStateViews() {
87+
String packageName = TermuxConstants.TERMUX_WIDGET_PACKAGE_NAME;
88+
String className = TermuxConstants.TERMUX_WIDGET_APP.TERMUX_WIDGET_LAUNCHER_ACTIVITY_NAME;
89+
90+
TextView changeLauncherActivityStateTextView = findViewById(R.id.textview_change_launcher_activity_state_details);
91+
changeLauncherActivityStateTextView.setText(MarkdownUtils.getSpannedMarkdownText(this,
92+
getString(R.string.msg_change_launcher_activity_state_info, packageName, getClass().getName())));
93+
94+
Button changeLauncherActivityStateButton = findViewById(R.id.button_change_launcher_activity_state);
95+
String stateChangeMessage;
96+
boolean newState;
97+
98+
Boolean currentlyDisabled = PackageUtils.isComponentDisabled(this,
99+
packageName, className, false);
100+
if (currentlyDisabled == null) {
101+
Logger.logError(LOG_TAG, "Failed to check if \"" + packageName + "/" + className + "\" launcher activity is disabled");
102+
changeLauncherActivityStateButton.setEnabled(false);
103+
changeLauncherActivityStateButton.setAlpha(.5f);
104+
changeLauncherActivityStateButton.setText(com.termux.shared.R.string.action_disable_launcher_icon);
105+
changeLauncherActivityStateButton.setOnClickListener(null);
106+
return;
107+
}
108+
109+
changeLauncherActivityStateButton.setEnabled(true);
110+
changeLauncherActivityStateButton.setAlpha(1f);
111+
if (currentlyDisabled) {
112+
changeLauncherActivityStateButton.setText(com.termux.shared.R.string.action_enable_launcher_icon);
113+
stateChangeMessage = getString(com.termux.shared.R.string.msg_enabling_launcher_icon, TermuxConstants.TERMUX_WIDGET_APP_NAME);
114+
newState = true;
115+
} else {
116+
changeLauncherActivityStateButton.setText(com.termux.shared.R.string.action_disable_launcher_icon);
117+
stateChangeMessage = getString(com.termux.shared.R.string.msg_disabling_launcher_icon, TermuxConstants.TERMUX_WIDGET_APP_NAME);
118+
newState = false;
119+
}
120+
121+
changeLauncherActivityStateButton.setOnClickListener(v -> {
122+
Logger.logInfo(LOG_TAG, stateChangeMessage);
123+
String errmsg = PackageUtils.setComponentState(this,
124+
packageName, className, newState, stateChangeMessage, true);
125+
if (errmsg == null)
126+
setChangeLauncherActivityStateViews();
127+
else
128+
Logger.logError(LOG_TAG, errmsg);
93129
});
94130
}
95131

@@ -100,22 +136,28 @@ private void setDynamicShortcutsViews() {
100136
dynamicShortcutsLinearLayout.setVisibility(View.VISIBLE);
101137

102138
TextView dynamicShortcutsInfoTextView = findViewById(R.id.textview_dynamic_shortcuts_info);
103-
dynamicShortcutsInfoTextView.setText(getString(R.string.msg_dynamic_shortcuts_info,
104-
TermuxFileUtils.getUnExpandedTermuxPath(TERMUX_WIDGET_DYNAMIC_SHORTCUTS_DIR_PATH)));
105-
106-
Button createDynamicShortcutsButton = findViewById(R.id.btn_create_dynamic_shortcuts);
107-
createDynamicShortcutsButton.setOnClickListener(v -> createDynamicShortcuts(TermuxWidgetActivity.this));
108-
109-
Button removeDynamicShortcutsButton = findViewById(R.id.btn_remove_dynamic_shortcuts);
110-
removeDynamicShortcutsButton.setOnClickListener(v -> removeDynamicShortcuts(TermuxWidgetActivity.this));
139+
dynamicShortcutsInfoTextView.setText(MarkdownUtils.getSpannedMarkdownText(this,
140+
getString(R.string.msg_dynamic_shortcuts_info,
141+
TermuxFileUtils.getUnExpandedTermuxPath(TERMUX_WIDGET_DYNAMIC_SHORTCUTS_DIR_PATH))));
142+
143+
Button createDynamicShortcutsButton = findViewById(R.id.button_create_dynamic_shortcuts);
144+
createDynamicShortcutsButton.setOnClickListener(v -> createDynamicShortcuts(this));
145+
146+
Button removeDynamicShortcutsButton = findViewById(R.id.button_remove_dynamic_shortcuts);
147+
removeDynamicShortcutsButton.setOnClickListener(v -> removeDynamicShortcuts(this));
111148
} else {
112149
dynamicShortcutsLinearLayout.setVisibility(View.GONE);
113150
}
114151
}
115152

116153
private void setRefreshAllWidgetsViews() {
117-
Button refreshAllWidgetsIconButton = findViewById(R.id.btn_refresh_all_widgets);
118-
refreshAllWidgetsIconButton.setOnClickListener(v -> TermuxWidgetProvider.sendIntentToRefreshAllWidgets(TermuxWidgetActivity.this, LOG_TAG));
154+
Button refreshAllWidgetsIconButton = findViewById(R.id.button_refresh_all_widgets);
155+
refreshAllWidgetsIconButton.setOnClickListener(
156+
v -> sendIntentToRefreshAllWidgets());
157+
}
158+
159+
public void sendIntentToRefreshAllWidgets() {
160+
TermuxWidgetProvider.sendIntentToRefreshAllWidgets(this, LOG_TAG);
119161
}
120162

121163
@RequiresApi(Build.VERSION_CODES.N_MR1)

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
6-
android:layout_height="match_parent" >
6+
android:layout_height="match_parent"
7+
tools:context=".activities.TermuxWidgetMainActivity">
78
<LinearLayout
89
android:orientation="vertical"
910
android:layout_width="match_parent"
1011
android:layout_height="wrap_content"
1112
android:paddingTop="@dimen/activity_vertical_margin"
1213
android:paddingBottom="@dimen/activity_vertical_margin"
1314
android:paddingLeft="@dimen/activity_horizontal_margin"
14-
android:paddingRight="@dimen/activity_horizontal_margin"
15-
tools:context=".activities.TermuxWidgetActivity">
15+
android:paddingRight="@dimen/activity_horizontal_margin">
1616

1717
<com.google.android.material.textview.MaterialTextView
1818
android:id="@+id/textview_plugin_info"
@@ -47,6 +47,7 @@
4747
android:textStyle="normal"
4848
android:textColor="?android:textColorPrimary"
4949
android:textColorLink="?android:textColorLink"
50+
android:textIsSelectable="true"
5051
android:autoLink="web"/>
5152

5253
</LinearLayout>
@@ -56,26 +57,25 @@
5657
<View style="@style/ViewDivider"/>
5758

5859
<com.google.android.material.textview.MaterialTextView
59-
android:id="@+id/textview_disable_launcher_icon_details"
60+
android:id="@+id/textview_change_launcher_activity_state_details"
6061
android:layout_width="match_parent"
6162
android:layout_height="wrap_content"
6263
android:paddingBottom="@dimen/activity_vertical_margin"
6364
android:gravity="start|center_vertical"
64-
android:text="@string/msg_disable_launcher_icon_info"
6565
android:textSize="14sp"
6666
android:textStyle="normal"
6767
android:textColor="?android:textColorPrimary"
6868
android:textColorLink="?android:textColorLink"
69+
android:textIsSelectable="true"
6970
android:autoLink="web"/>
7071

7172
<com.google.android.material.button.MaterialButton
7273
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
73-
android:id="@+id/btn_disable_launcher_icon"
74+
android:id="@+id/button_change_launcher_activity_state"
7475
android:layout_width="wrap_content"
7576
android:layout_height="wrap_content"
7677
android:layout_gravity="end"
7778
android:gravity="center"
78-
android:text="@string/action_disable_launcher_icon"
7979
android:textSize="12sp"
8080
android:textColor="?android:textColorPrimary"
8181
app:strokeColor="?android:textColorPrimary"
@@ -92,7 +92,7 @@
9292
<View style="@style/ViewDivider"/>
9393

9494
<com.google.android.material.textview.MaterialTextView
95-
android:id="@+id/textview_dynamic_shortcuts_info"
95+
android:id="@+id/textview_change_launcher_activity_state_details"
9696
android:layout_width="match_parent"
9797
android:layout_height="wrap_content"
9898
android:paddingBottom="@dimen/activity_vertical_margin"
@@ -101,6 +101,7 @@
101101
android:textStyle="normal"
102102
android:textColor="?android:textColorPrimary"
103103
android:textColorLink="?android:textColorLink"
104+
android:textIsSelectable="true"
104105
android:autoLink="web"/>
105106

106107
<LinearLayout
@@ -110,7 +111,7 @@
110111

111112
<com.google.android.material.button.MaterialButton
112113
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
113-
android:id="@+id/btn_remove_dynamic_shortcuts"
114+
android:id="@+id/button_remove_dynamic_shortcuts"
114115
android:layout_width="0dp"
115116
android:layout_height="wrap_content"
116117
android:layout_weight="1"
@@ -125,7 +126,7 @@
125126

126127
<com.google.android.material.button.MaterialButton
127128
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
128-
android:id="@+id/btn_create_dynamic_shortcuts"
129+
android:id="@+id/button_create_dynamic_shortcuts"
129130
android:layout_width="0dp"
130131
android:layout_height="wrap_content"
131132
android:layout_weight="1"
@@ -156,11 +157,12 @@
156157
android:textStyle="normal"
157158
android:textColor="?android:textColorPrimary"
158159
android:textColorLink="?android:textColorLink"
160+
android:textIsSelectable="true"
159161
android:autoLink="web"/>
160162

161163
<com.google.android.material.button.MaterialButton
162164
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
163-
android:id="@+id/btn_refresh_all_widgets"
165+
android:id="@+id/button_refresh_all_widgets"
164166
android:layout_width="wrap_content"
165167
android:layout_height="wrap_content"
166168
android:layout_gravity="end"

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,27 @@
88
<!ENTITY TERMUX_SHORTCUT_SCRIPT_ICONS_DIR_PATH_SHORT "~/.shortcuts/icons">
99
]>
1010

11-
<resources>EXTRA_SHORTCUT_INTENT
11+
<resources>
1212
<string name="app_name">&TERMUX_WIDGET_APP_NAME;</string>
1313
<string name="plugin_info">&TERMUX_WIDGET_APP_NAME; is a plugin app for the &TERMUX_APP_NAME; app.
1414
\n\nCheck &TERMUX_APP_NAME; app github %1$s and &TERMUX_WIDGET_APP_NAME; app github %2$s for more info.</string>
1515
<string name="plugin_api_help">Visit %1$s for more info on plugin usage.</string>
1616

17-
<!-- TermuxWidgetActivity -->
18-
<string name="msg_disable_launcher_icon_info">The &TERMUX_WIDGET_APP_NAME; app does not require
19-
a launcher icon/activity to function. You can optionally disable it if you want and
20-
can enabled in again either by reinstalling the app or from the &TERMUX_APP_NAME; app settings
21-
if the option has been implemented in your installed version."</string>
17+
<!-- TermuxWidgetMainActivity -->
18+
<string name="msg_change_launcher_activity_state_info">The &TERMUX_WIDGET_APP_NAME; app does not
19+
require a launcher activity/icon to function. You can optionally disable the launcher
20+
activity if you want and enable it again from the main activity if required.
21+
\n\nThe launcher activity is an alias for the current main activity of the app which can
22+
still be opened after disabling the launcher activity. The main activity can be opened
23+
from `&TERMUX_APP_NAME; app settings` -> `&TERMUX_WIDGET_APP_NAME;` -> `Open App` if the
24+
option has been implemented in your installed &TERMUX_APP_NAME; app version. Otherwise,
25+
running the `am start "%1$s/%2$s"` command in the &TERMUX_APP_NAME; app should open it.
26+
\n\nNote that on some devices the widgets created on launcher home may not function properly
27+
if the launcher activity is disabled and if the widgets have reverted to their default
28+
view and show the "Open &TERMUX_WIDGET_APP_NAME; app to refresh widget" message, then
29+
opening the launcher/main activity will be necessary to refresh the widgets. Additionally,
30+
if the launcher activity has been disabled and the widget is in the default view, then
31+
clicking the widget title may crash your launcher.</string>
2232

2333
<string name="msg_max_shortcuts_limit_info">The current max static and dynamic shortcuts limit
2434
for the &TERMUX_WIDGET_APP_NAME; app is %1$d.
@@ -40,11 +50,12 @@
4050

4151
<string name="msg_refresh_all_widgets_info">To manually refresh all widgets to show updated
4252
shortcuts, click the refresh button.
43-
\n\nThis may also be needed in some cases after app updates where widgets become non-responsive
44-
and do not show any shortcuts and refresh buttons of the widgets itself do not work either.</string>
45-
<string name="action_refresh_all_widgets">Refresh</string>
46-
47-
53+
\n\nThis may also be needed in some cases after app updates where widgets revert to their
54+
default view and become non-responsive and show the
55+
"Open &TERMUX_WIDGET_APP_NAME; app to refresh widget" message instead of showing the
56+
shortcuts. In this state, the refresh buttons of the widgets itself will not work. Just
57+
launching the &TERMUX_WIDGET_APP_NAME; app will also refresh the widget.</string>
58+
<string name="action_refresh_all_widgets">Refresh Widgets</string>
4859

4960
<!-- ShortcutFile -->
5061
<string name="msg_shortcut_icon_file_used">Using file at `%1$s` as shortcut icon file</string>

0 commit comments

Comments
 (0)