Skip to content

Commit b0d0b1f

Browse files
authored
Implement Widget UI (#20)
* Fix launching new intent from tile * Add widget resources, dimens, etc * Add widget layout * Apply layout from Widget service * Small code tidy up * Update Unit Tests
1 parent 19dcb86 commit b0d0b1f

File tree

17 files changed

+278
-26
lines changed

17 files changed

+278
-26
lines changed

app/src/main/java/com/kinandcarta/create/proxytoggle/feature/tile/ProxyTileService.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.kinandcarta.create.proxytoggle.feature.tile
22

3+
import android.content.Intent
34
import android.os.Build
45
import android.service.quicksettings.Tile
56
import android.service.quicksettings.TileService
@@ -40,7 +41,9 @@ class ProxyTileService : TileService() {
4041
deviceSettingsManager.enableProxy(lastUsedProxy)
4142
} else {
4243
// There is no last used Proxy, prompt the user to create one
43-
startActivityAndCollapse(MainActivity.getIntent(baseContext))
44+
startActivityAndCollapse(MainActivity.getIntent(baseContext).apply {
45+
flags = Intent.FLAG_ACTIVITY_NEW_TASK
46+
})
4447
}
4548
}
4649
updateTile()

app/src/main/java/com/kinandcarta/create/proxytoggle/feature/widget/ToggleWidgetProvider.kt

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import android.appwidget.AppWidgetProvider
66
import android.content.Context
77
import android.content.Intent
88
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
9-
import android.view.View
109
import android.widget.RemoteViews
10+
import androidx.core.content.ContextCompat
1111
import com.kinandcarta.create.proxytoggle.R
1212
import com.kinandcarta.create.proxytoggle.android.DeviceSettingsManager
1313
import com.kinandcarta.create.proxytoggle.model.Proxy
@@ -21,8 +21,6 @@ import javax.inject.Inject
2121
@AndroidEntryPoint
2222
class ToggleWidgetProvider : AppWidgetProvider() {
2323

24-
// TODO Handle the scenario when the user adds the widget before the proxy
25-
2624
companion object {
2725
private const val ACTION_PROXY_ENABLE = "Enable Proxy"
2826
private const val ACTION_PROXY_DISABLE = "Disable Proxy"
@@ -51,28 +49,47 @@ class ToggleWidgetProvider : AppWidgetProvider() {
5149
buildDisabledView(remoteView, context)
5250
}
5351

52+
val intent = MainActivity.getIntent(context).apply { flags = FLAG_ACTIVITY_NEW_TASK }
53+
remoteView.setOnClickPendingIntent(
54+
R.id.settings, PendingIntent.getActivity(context, 0, intent, 0)
55+
)
56+
5457
appWidgetManager.updateAppWidget(appWidgetId, remoteView)
5558
}
5659
}
5760

5861
private fun buildDisabledView(remoteView: RemoteViews, context: Context) {
5962
remoteView.apply {
60-
setTextViewText(R.id.button, "Enable")
61-
setViewVisibility(R.id.proxy, View.GONE)
63+
val lastUsedProxy = appSettings.lastUsedProxy
64+
if (lastUsedProxy.isEnabled) {
65+
setTextViewText(R.id.address, lastUsedProxy.address)
66+
setTextViewText(R.id.port, lastUsedProxy.port)
67+
} else {
68+
setTextViewText(R.id.address, context.getString(R.string.widget_not_set))
69+
setTextViewText(R.id.port, context.getString(R.string.widget_not_set))
70+
}
71+
setTextViewText(R.id.status, context.getString(R.string.proxy_status_disabled))
72+
setTextColor(
73+
R.id.status,
74+
ContextCompat.getColor(context, R.color.widget_label_disabled)
75+
)
76+
setImageViewResource(R.id.toggle, R.drawable.widget_toggle_disabled)
6277
setOnClickPendingIntent(
63-
R.id.button,
78+
R.id.toggle,
6479
ACTION_PROXY_ENABLE.asPendingIntent(context)
6580
)
6681
}
6782
}
6883

6984
private fun buildEnabledView(remoteView: RemoteViews, proxy: Proxy, context: Context) {
7085
remoteView.apply {
71-
setTextViewText(R.id.button, "Disable")
72-
setViewVisibility(R.id.proxy, View.VISIBLE)
73-
setTextViewText(R.id.proxy, proxy.toString())
86+
setTextViewText(R.id.address, proxy.address)
87+
setTextViewText(R.id.port, proxy.port)
88+
setTextViewText(R.id.status, context.getString(R.string.proxy_status_enabled))
89+
setTextColor(R.id.status, ContextCompat.getColor(context, R.color.widget_label_enabled))
90+
setImageViewResource(R.id.toggle, R.drawable.widget_toggle_enabled)
7491
setOnClickPendingIntent(
75-
R.id.button,
92+
R.id.toggle,
7693
ACTION_PROXY_DISABLE.asPendingIntent(context)
7794
)
7895
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="36dp"
3+
android:height="41dp"
4+
android:viewportWidth="36"
5+
android:viewportHeight="41">
6+
<path
7+
android:pathData="m23.1248,8.5195c6.286,2.112 10.509,7.946 10.509,14.515c0,8.451 -6.953,15.327 -15.5,15.327s-15.5,-6.876 -15.5,-15.327c0,-6.554 4.21,-12.383 10.477,-14.505c0.196,-0.066 0.41,0.037 0.476,0.231c0.067,0.194 -0.037,0.405 -0.233,0.47c-5.963,2.02 -9.97,7.567 -9.97,13.804c0,8.042 6.617,14.586 14.75,14.586s14.75,-6.544 14.75,-14.586c0,-6.252 -4.019,-11.803 -10,-13.813c-0.196,-0.066 -0.301,-0.277 -0.235,-0.47c0.067,-0.194 0.28,-0.299 0.476,-0.232zM18.1348,2.3615c0.206,0 0.374,0.166 0.374,0.37l0,21.366c0,0.205 -0.168,0.37 -0.375,0.37s-0.375,-0.165 -0.375,-0.37l0,-21.365c0,-0.205 0.168,-0.371 0.375,-0.371l0.001,0z"
8+
android:strokeWidth="2.6"
9+
android:fillColor="#F1F1F2"
10+
android:fillType="evenOdd"
11+
android:strokeColor="#F1F1F2"/>
12+
</vector>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="36dp"
3+
android:height="41dp"
4+
android:viewportWidth="36"
5+
android:viewportHeight="41">
6+
<path
7+
android:pathData="m23.1248,8.5195c6.286,2.112 10.509,7.946 10.509,14.515c0,8.451 -6.953,15.327 -15.5,15.327s-15.5,-6.876 -15.5,-15.327c0,-6.554 4.21,-12.383 10.477,-14.505c0.196,-0.066 0.41,0.037 0.476,0.231c0.067,0.194 -0.037,0.405 -0.233,0.47c-5.963,2.02 -9.97,7.567 -9.97,13.804c0,8.042 6.617,14.586 14.75,14.586s14.75,-6.544 14.75,-14.586c0,-6.252 -4.019,-11.803 -10,-13.813c-0.196,-0.066 -0.301,-0.277 -0.235,-0.47c0.067,-0.194 0.28,-0.299 0.476,-0.232zM18.1348,2.3615c0.206,0 0.374,0.166 0.374,0.37l0,21.366c0,0.205 -0.168,0.37 -0.375,0.37s-0.375,-0.165 -0.375,-0.37l0,-21.365c0,-0.205 0.168,-0.371 0.375,-0.371l0.001,0z"
8+
android:strokeWidth="2.6"
9+
android:fillColor="#a080fb"
10+
android:fillType="evenOdd"
11+
android:strokeColor="#a080fb"/>
12+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z"/>
9+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<solid android:color="@color/dark_alpha_80" />
4+
<corners android:radius="10dp" />
5+
</shape>
64.8 KB
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item>
4+
<shape>
5+
<gradient
6+
android:centerColor="@color/toggle_glow_disabled"
7+
android:endColor="@android:color/transparent"
8+
android:gradientRadius="@dimen/widget_toggle_gradient_radius"
9+
android:startColor="@android:color/transparent"
10+
android:type="radial" />
11+
<padding
12+
android:bottom="@dimen/widget_toggle_gradient_padding"
13+
android:left="@dimen/widget_toggle_gradient_padding"
14+
android:right="@dimen/widget_toggle_gradient_padding"
15+
android:top="@dimen/widget_toggle_gradient_padding" />
16+
</shape>
17+
</item>
18+
<item>
19+
<shape android:shape="oval">
20+
<size
21+
android:width="@dimen/toggle_shape_size"
22+
android:height="@dimen/toggle_shape_size" />
23+
<solid android:color="@color/widget_toggle_background" />
24+
</shape>
25+
</item>
26+
<item>
27+
<inset
28+
android:drawable="@drawable/ic_power_widget_disabled"
29+
android:insetLeft="@dimen/toggle_inset"
30+
android:insetTop="@dimen/toggle_inset_small"
31+
android:insetRight="@dimen/toggle_inset"
32+
android:insetBottom="@dimen/toggle_inset" />
33+
</item>
34+
</layer-list>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
3+
<item>
4+
<shape>
5+
<gradient
6+
android:centerColor="@color/toggle_glow_enabled"
7+
android:endColor="@android:color/transparent"
8+
android:gradientRadius="@dimen/widget_toggle_gradient_radius"
9+
android:startColor="@android:color/transparent"
10+
android:type="radial" />
11+
<padding
12+
android:bottom="@dimen/widget_toggle_gradient_padding"
13+
android:left="@dimen/widget_toggle_gradient_padding"
14+
android:right="@dimen/widget_toggle_gradient_padding"
15+
android:top="@dimen/widget_toggle_gradient_padding" />
16+
</shape>
17+
</item>
18+
<item>
19+
<shape android:shape="oval">
20+
<size
21+
android:width="@dimen/toggle_shape_size"
22+
android:height="@dimen/toggle_shape_size" />
23+
<solid android:color="@color/widget_toggle_background" />
24+
</shape>
25+
</item>
26+
<item>
27+
<inset
28+
android:drawable="@drawable/ic_power_widget_enabled"
29+
android:insetLeft="@dimen/toggle_inset"
30+
android:insetTop="@dimen/toggle_inset_small"
31+
android:insetRight="@dimen/toggle_inset"
32+
android:insetBottom="@dimen/toggle_inset" />
33+
</item>
34+
</layer-list>
Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,110 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
5-
android:layout_height="match_parent"
6-
android:orientation="vertical">
5+
android:layout_height="wrap_content"
6+
android:background="@drawable/widget_background"
7+
android:gravity="center_vertical"
8+
android:minWidth="@dimen/widget_min_resizable_width"
9+
android:orientation="horizontal"
10+
android:paddingHorizontal="@dimen/widget_outer_horizontal_padding"
11+
android:paddingVertical="@dimen/widget_default_margin">
712

8-
<Button
9-
android:id="@+id/button"
13+
<ImageView
14+
android:id="@+id/settings"
1015
android:layout_width="wrap_content"
1116
android:layout_height="wrap_content"
12-
tools:text="Enable/Disable" />
17+
android:layout_alignParentTop="true"
18+
android:layout_alignParentEnd="true"
19+
android:contentDescription="@string/a11y_settings"
20+
android:src="@drawable/ic_settings"
21+
android:tint="@color/bluey_grey" />
1322

14-
<TextView
15-
android:id="@+id/proxy"
16-
android:layout_width="wrap_content"
23+
<ImageView
24+
android:id="@+id/toggle"
25+
android:layout_width="@dimen/widget_toggle_icon_size"
26+
android:layout_height="@dimen/widget_toggle_icon_size"
27+
android:layout_alignParentStart="true"
28+
android:layout_centerVertical="true"
29+
android:contentDescription="@string/a11y_toggle_proxy"
30+
android:src="@drawable/toggle"
31+
tools:src="@drawable/widget_toggle_enabled"/>
32+
33+
<LinearLayout
34+
android:layout_width="match_parent"
1735
android:layout_height="wrap_content"
18-
tools:text="192.168.1.1:8888" />
36+
android:layout_centerVertical="true"
37+
android:layout_marginStart="@dimen/widget_icon_margin"
38+
android:layout_toEndOf="@id/toggle"
39+
android:orientation="vertical">
40+
41+
<TextView
42+
android:id="@+id/status"
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content"
45+
android:fontFamily="@font/dmsans"
46+
android:letterSpacing="0.2"
47+
android:textAllCaps="true"
48+
android:textStyle="bold"
49+
tools:textColor="@color/periwinkle"
50+
tools:text="Enabled" />
51+
52+
<LinearLayout
53+
android:layout_width="match_parent"
54+
android:layout_height="wrap_content"
55+
android:layout_marginTop="@dimen/widget_default_margin"
56+
android:baselineAligned="false"
57+
android:orientation="horizontal">
58+
59+
<LinearLayout
60+
android:layout_width="wrap_content"
61+
android:layout_height="wrap_content"
62+
android:orientation="vertical">
63+
64+
<TextView
65+
android:layout_width="wrap_content"
66+
android:layout_height="wrap_content"
67+
android:fontFamily="@font/dmsans"
68+
android:text="@string/widget_label_ip_address"
69+
android:textColor="@color/white"
70+
android:textSize="@dimen/widget_label_text_size"
71+
tools:ignore="SmallSp" />
72+
73+
<TextView
74+
android:id="@+id/address"
75+
android:layout_width="wrap_content"
76+
android:layout_height="wrap_content"
77+
android:fontFamily="@font/dmsans"
78+
android:textColor="@color/white"
79+
tools:text="10.10.5.15" />
80+
81+
</LinearLayout>
82+
83+
<LinearLayout
84+
android:layout_width="wrap_content"
85+
android:layout_height="wrap_content"
86+
android:layout_marginStart="@dimen/widget_xlarge_margin"
87+
android:orientation="vertical">
88+
89+
<TextView
90+
android:layout_width="wrap_content"
91+
android:layout_height="wrap_content"
92+
android:fontFamily="@font/dmsans"
93+
android:text="@string/widget_label_port"
94+
android:textColor="@color/white"
95+
android:textSize="@dimen/widget_label_text_size"
96+
tools:ignore="SmallSp" />
97+
98+
<TextView
99+
android:id="@+id/port"
100+
android:layout_width="wrap_content"
101+
android:layout_height="wrap_content"
102+
android:fontFamily="@font/dmsans"
103+
android:textColor="@color/white"
104+
tools:text="8888" />
105+
</LinearLayout>
106+
107+
</LinearLayout>
19108

20-
</LinearLayout>
109+
</LinearLayout>
110+
</RelativeLayout>

0 commit comments

Comments
 (0)