Skip to content

Commit 2eb0ba9

Browse files
author
wangshouheng
committed
feat: add new utils
1 parent 8bc2e9c commit 2eb0ba9

File tree

14 files changed

+936
-165
lines changed

14 files changed

+936
-165
lines changed

sample/app/src/main/java/me/shouheng/samples/activity/TestActivityHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import android.net.Uri
66
import android.os.Build.VERSION
77
import android.os.Build.VERSION_CODES
88
import android.os.Bundle
9-
import androidx.appcompat.app.AppCompatActivity
109
import android.view.View
10+
import androidx.appcompat.app.AppCompatActivity
1111
import me.shouheng.samples.R
1212
import me.shouheng.utils.constant.ActivityDirection
1313
import me.shouheng.utils.ktx.attrColorOf

sample/app/src/main/java/me/shouheng/samples/data/TestTimeUtilsActivity.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package me.shouheng.samples.data;
22

33
import androidx.appcompat.app.AppCompatActivity;
4+
5+
import android.content.ClipboardManager;
6+
import android.content.Context;
47
import android.os.Bundle;
8+
import android.view.View;
59
import android.widget.TextView;
610

711
import java.text.SimpleDateFormat;
812
import java.util.Locale;
913

1014
import me.shouheng.samples.R;
1115
import me.shouheng.utils.data.TimeUtils;
16+
import me.shouheng.utils.ui.ToastUtils;
1217

1318
import static android.text.format.DateUtils.*;
1419

@@ -21,7 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
2126

2227
long now = TimeUtils.now();
2328
TextView tv = findViewById(R.id.tv);
24-
String info = TimeUtils.toString(now, new SimpleDateFormat("yyyy MMM dd E hh:mm a", Locale.getDefault())) + "\n"
29+
final String info = TimeUtils.toString(now, new SimpleDateFormat("yyyy MMM dd E hh:mm a", Locale.getDefault())) + "\n"
2530
+ "FORMAT_ABBREV_TIME: " + TimeUtils.toString(now, FORMAT_ABBREV_TIME) + "\n"
2631
+ "FORMAT_ABBREV_WEEKDAY: " + TimeUtils.toString(now, FORMAT_ABBREV_WEEKDAY) + "\n"
2732
+ "FORMAT_ABBREV_MONTH: " + TimeUtils.toString(now, FORMAT_ABBREV_MONTH) + "\n"
@@ -43,7 +48,27 @@ protected void onCreate(Bundle savedInstanceState) {
4348
+ "FORMAT_SHOW_TIME | FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL: \n"
4449
+ TimeUtils.toString(now, FORMAT_SHOW_TIME | FORMAT_SHOW_DATE | FORMAT_ABBREV_ALL) + "\n"
4550
+ "FORMAT_24HOUR | FORMAT_SHOW_DATE | FORMAT_SHOW_TIME | FORMAT_SHOW_YEAR | LENGTH_LONG | FORMAT_ABBREV_MONTH: \n"
46-
+ TimeUtils.toString(now, FORMAT_24HOUR | FORMAT_SHOW_DATE | FORMAT_SHOW_TIME | FORMAT_SHOW_YEAR | LENGTH_LONG | FORMAT_ABBREV_MONTH);
51+
+ TimeUtils.toString(now, FORMAT_24HOUR | FORMAT_SHOW_DATE | FORMAT_SHOW_TIME | FORMAT_SHOW_YEAR | LENGTH_LONG | FORMAT_ABBREV_MONTH) + "\n"
52+
+ "BEGIN OF TODAY: " + TimeUtils.beginOfToday() + "\n"
53+
+ "BEGIN OF WEEK (SUN): " + TimeUtils.beginOfWeek(true) + "\n"
54+
+ "BEGIN OF WEEK: " + TimeUtils.beginOfWeek(false) + "\n"
55+
+ "BEGIN OF MONTH: " + TimeUtils.beginOfMonth() + "\n"
56+
+ "BEGIN OF YEAR: " + TimeUtils.beginOfYear() + "\n"
57+
+ "END OF TODAY: " + TimeUtils.endOfToday() + "\n"
58+
+ "END OF WEEK (SUN): " + TimeUtils.endOfWeek(true) + "\n"
59+
+ "END OF WEEK: " + TimeUtils.endOfWeek(false) + "\n"
60+
+ "END OF MONTH: " + TimeUtils.endOfMonth() + "\n"
61+
+ "END OF YEAR: " + TimeUtils.endOfYear() + "\n"
62+
;
4763
tv.setText(info);
64+
tv.setOnLongClickListener(new View.OnLongClickListener() {
65+
@Override
66+
public boolean onLongClick(View v) {
67+
ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
68+
clipboardManager.setText(info);
69+
ToastUtils.showShort("copied");
70+
return true;
71+
}
72+
});
4873
}
4974
}

sample/app/src/main/java/me/shouheng/samples/intent/TestIntentActivity.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import androidx.core.content.FileProvider;
1010
import androidx.appcompat.app.AlertDialog;
1111
import android.view.View;
12+
import android.widget.TextView;
1213
import android.widget.Toast;
1314

1415
import java.io.File;
1516
import java.util.List;
1617

1718
import me.shouheng.samples.R;
1819
import me.shouheng.samples.common.BaseActivity;
20+
import me.shouheng.utils.app.AppUtils;
1921
import me.shouheng.utils.app.IntentUtils;
2022
import me.shouheng.utils.permission.Permission;
2123
import me.shouheng.utils.permission.PermissionUtils;
@@ -32,6 +34,13 @@ protected void onCreate(Bundle savedInstanceState) {
3234
setContentView(R.layout.activity_test_intent);
3335
}
3436

37+
@Override
38+
protected void onResume() {
39+
super.onResume();
40+
TextView textView = findViewById(R.id.tv_notification_status);
41+
textView.setText("Notification status : " + (AppUtils.areNotificationEnabled() ? "on" : "off"));
42+
}
43+
3544
public void testLaunchApp(View v) {
3645
startActivity(IntentUtils.getLaunchAppIntent(getPackageName()));
3746
}
@@ -101,6 +110,11 @@ public void testGetAppInfo(View v) {
101110
.create().show();
102111
}
103112

113+
public void openNotification(View v) {
114+
Intent intent = IntentUtils.getNotificationSettingIntent(true);
115+
startActivity(intent);
116+
}
117+
104118
@Override
105119
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
106120
super.onActivityResult(requestCode, resultCode, data);

sample/app/src/main/java/me/shouheng/samples/store/StorageActivity.kt

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import me.shouheng.utils.store.IOUtils
2222
import me.shouheng.utils.store.PathUtils
2323
import me.shouheng.utils.store.SPUtils
2424
import java.io.File
25-
import java.io.IOException
26-
import java.io.OutputStream
2725
import java.util.*
2826

2927
/** To make a sample for storage usage on Android 11. */
@@ -120,11 +118,11 @@ class StorageActivity : AppCompatActivity() {
120118
val root = DocumentFile.fromTreeUri(this, uri)
121119
var doc = createOrExistsFile(root, "test_a", "application/txt", "${System.currentTimeMillis()}.txt")
122120
var ous = this.contentResolver.openOutputStream(doc!!.uri)
123-
var ret = writeToOutputStream(ous, "sample a")
121+
var ret = IOUtils.write2Stream(ous, "sample a")
124122
toast("Result: $ret")
125123
doc = createOrExistsFile(root, "test_a/test_a_b", "application/txt", "${System.currentTimeMillis()}.txt")
126124
ous = this.contentResolver.openOutputStream(doc!!.uri)
127-
ret = writeToOutputStream(ous, "sample a b")
125+
ret = IOUtils.write2Stream(ous, "sample a b")
128126
toast("Result: $ret")
129127
} catch (e: Exception) {
130128
e.printStackTrace()
@@ -140,12 +138,12 @@ class StorageActivity : AppCompatActivity() {
140138
val name = "sample.txt"
141139
val doc = createOrExistsFile(root, "test_a", "application/txt", name)
142140
val ous = this.contentResolver.openOutputStream(doc!!.uri)
143-
val ret = writeToOutputStream(ous, "sample a")
141+
val ret = IOUtils.write2Stream(ous, "sample a")
144142
toast("Write succeed: $ret")
145143
val file = getFile(root, "test_a", name)
146144
val ins = contentResolver.openInputStream(file!!.uri)
147-
val bytes = IOUtils.is2Bytes(ins)
148-
toast("Read: ${String(bytes)}")
145+
val text = IOUtils.is2String(ins)
146+
toast("Read: $text")
149147
} catch (e: Exception) {
150148
e.printStackTrace()
151149
toast("failed!")
@@ -226,18 +224,6 @@ class StorageActivity : AppCompatActivity() {
226224
toast(Arrays.toString(files))
227225
}
228226

229-
private fun writeToOutputStream(ous: OutputStream?, text: String): Boolean {
230-
return try {
231-
ous?.write(text.toByteArray())
232-
true
233-
} catch (e: IOException) {
234-
e.printStackTrace()
235-
false
236-
} finally {
237-
IOUtils.safeCloseAll(ous)
238-
}
239-
}
240-
241227
private fun createOrExistsFile(
242228
root: DocumentFile?,
243229
directoryPath: String,

sample/app/src/main/java/me/shouheng/samples/ui/TestAnimUtilsActivity.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import me.shouheng.samples.R
88
import me.shouheng.utils.ktx.*
99
import me.shouheng.utils.ui.AnimUtils
1010
import me.shouheng.utils.ui.ViewUtils
11+
import android.view.animation.Animation.RELATIVE_TO_SELF
1112

1213
/**
1314
* Anim utils test
@@ -21,19 +22,24 @@ class TestAnimUtilsActivity : AppCompatActivity() {
2122
super.onCreate(savedInstanceState)
2223
setContentView(R.layout.activity_anim_utils)
2324
obj = findViewById(R.id.v)
24-
findViewById<View>(R.id.btn_fade_in).setOnClickListener { obj?.fadeIn(2000, null, true) }
25-
findViewById<View>(R.id.btn_fade_out).setOnClickListener { obj?.fadeOut(2000, null, true) }
26-
findViewById<View>(R.id.btn_slide_in).setOnClickListener { obj?.slideIn(2000, null, true, AnimUtils.UIDirection.RIGHT_TO_LEFT) }
27-
findViewById<View>(R.id.btn_slide_out).setOnClickListener { obj?.slideOut(2000, null, true, AnimUtils.UIDirection.LEFT_TO_RIGHT) }
28-
findViewById<View>(R.id.btn_shinning).setOnClickListener { obj?.shining(2888, 6, 0f, 0.66f, 1.0f, 0f) }
25+
findViewById<View>(R.id.btn_fade_in).onDebouncedClick { obj?.fadeIn(2000, null, true) }
26+
findViewById<View>(R.id.btn_fade_out).onDebouncedClick { obj?.fadeOut(2000, null, true) }
27+
findViewById<View>(R.id.btn_slide_in).onDebouncedClick { obj?.slideIn(2000, null, true, AnimUtils.UIDirection.RIGHT_TO_LEFT) }
28+
findViewById<View>(R.id.btn_slide_out).onDebouncedClick { obj?.slideOut(2000, null, true, AnimUtils.UIDirection.LEFT_TO_RIGHT) }
29+
findViewById<View>(R.id.btn_shinning).onDebouncedClick { obj?.shining(2888, 6, 0f, 0.66f, 1.0f, 0f) }
2930
}
3031

3132
fun shake(view: View?) {
3233
obj?.shake()
3334
}
3435

36+
fun shakeUpDown(view: View?) {
37+
obj?.shake(AnimUtils.UIDirection.BOTTOM_TO_TOP)
38+
}
39+
3540
fun changeColor(view: View?) {
36-
changeColor(Color.RED, Color.BLUE, 3000) { obj!!.setBackgroundColor(it) }
41+
obj?.changeBackgroundColor(Color.RED, Color.BLUE, 3000)
42+
// changeColor(Color.RED, Color.BLUE, 3000) { obj!!.setBackgroundColor(it) }
3743
// AnimUtils.changeColor(Color.RED, Color.BLUE, 3000) { }
3844
}
3945

@@ -64,4 +70,12 @@ class TestAnimUtilsActivity : AppCompatActivity() {
6470
fun rotate(view: View) {
6571
obj?.rotate(2_000)
6672
}
73+
74+
fun scale(view: View) {
75+
obj?.scales(500L, 1f, .5f, 1f)
76+
}
77+
78+
fun scales(view: View) {
79+
obj?.scales(500L, .5f, RELATIVE_TO_SELF, 1f, .5f, 1.5f, 0.5f, 1f)
80+
}
6781
}

sample/app/src/main/res/layout/activity_anim_utils.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@
2222

2323
</FrameLayout>
2424

25+
<Button
26+
android:text="Scale"
27+
android:onClick="scale"
28+
android:textAllCaps="false"
29+
android:layout_width="match_parent"
30+
android:layout_height="wrap_content"/>
31+
32+
<Button
33+
android:text="Scales"
34+
android:onClick="scales"
35+
android:textAllCaps="false"
36+
android:layout_width="match_parent"
37+
android:layout_height="wrap_content"/>
38+
2539
<Button
2640
android:id="@+id/btn_fade_in"
2741
android:text="Fade in"
@@ -64,6 +78,13 @@
6478
android:layout_width="match_parent"
6579
android:layout_height="wrap_content"/>
6680

81+
<Button
82+
android:text="Shake (up-down)"
83+
android:onClick="shakeUpDown"
84+
android:textAllCaps="false"
85+
android:layout_width="match_parent"
86+
android:layout_height="wrap_content"/>
87+
6788
<Button
6889
android:text="Change Color"
6990
android:onClick="changeColor"
Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,76 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
2+
<androidx.core.widget.NestedScrollView
3+
xmlns:android="http://schemas.android.com/apk/res/android"
44
xmlns:tools="http://schemas.android.com/tools"
5-
android:orientation="vertical"
5+
tools:context="me.shouheng.samples.intent.TestIntentActivity"
66
android:layout_width="match_parent"
7-
android:layout_height="match_parent"
8-
tools:context="me.shouheng.samples.intent.TestIntentActivity">
9-
10-
<androidx.appcompat.widget.AppCompatButton
11-
android:text="启动当前应用"
12-
android:onClick="testLaunchApp"
13-
style="@style/MenuButton"/>
14-
15-
<androidx.appcompat.widget.AppCompatButton
16-
android:text="启动当前应用设置页面"
17-
android:onClick="testLaunchSetting"
18-
style="@style/MenuButton"/>
19-
20-
<androidx.appcompat.widget.AppCompatButton
21-
android:text="分享文本信息"
22-
android:onClick="testShareText"
23-
style="@style/MenuButton"/>
24-
25-
<androidx.appcompat.widget.AppCompatButton
26-
android:text="拨打 10086"
27-
android:onClick="testDial"
28-
style="@style/MenuButton"/>
29-
30-
<androidx.appcompat.widget.AppCompatButton
31-
android:text="发送消息给 10086"
32-
android:onClick="testSendSms"
33-
style="@style/MenuButton"/>
34-
35-
<androidx.appcompat.widget.AppCompatButton
36-
android:text="发送邮件"
37-
android:onClick="testSendEmail"
38-
style="@style/MenuButton"/>
39-
40-
<androidx.appcompat.widget.AppCompatButton
41-
android:text="打开百度网页"
42-
android:onClick="testOpenWeb"
43-
style="@style/MenuButton"/>
44-
45-
<androidx.appcompat.widget.AppCompatButton
46-
android:text="打开应用市场"
47-
android:onClick="testOpenMarket"
48-
style="@style/MenuButton"/>
49-
50-
<androidx.appcompat.widget.AppCompatButton
51-
android:text="拍照"
52-
android:onClick="testCapture"
53-
style="@style/MenuButton"/>
54-
55-
<androidx.appcompat.widget.AppCompatButton
56-
android:text="获取可以执行分享文本意图的应用的信息"
57-
android:onClick="testGetAppInfo"
58-
style="@style/MenuButton"/>
59-
60-
</LinearLayout>
7+
android:layout_height="match_parent">
8+
9+
<LinearLayout
10+
android:orientation="vertical"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent">
13+
14+
<androidx.appcompat.widget.AppCompatButton
15+
android:text="启动当前应用"
16+
android:onClick="testLaunchApp"
17+
style="@style/MenuButton"/>
18+
19+
<androidx.appcompat.widget.AppCompatButton
20+
android:text="启动当前应用设置页面"
21+
android:onClick="testLaunchSetting"
22+
style="@style/MenuButton"/>
23+
24+
<androidx.appcompat.widget.AppCompatButton
25+
android:text="分享文本信息"
26+
android:onClick="testShareText"
27+
style="@style/MenuButton"/>
28+
29+
<androidx.appcompat.widget.AppCompatButton
30+
android:text="拨打 10086"
31+
android:onClick="testDial"
32+
style="@style/MenuButton"/>
33+
34+
<androidx.appcompat.widget.AppCompatButton
35+
android:text="发送消息给 10086"
36+
android:onClick="testSendSms"
37+
style="@style/MenuButton"/>
38+
39+
<androidx.appcompat.widget.AppCompatButton
40+
android:text="发送邮件"
41+
android:onClick="testSendEmail"
42+
style="@style/MenuButton"/>
43+
44+
<androidx.appcompat.widget.AppCompatButton
45+
android:text="打开百度网页"
46+
android:onClick="testOpenWeb"
47+
style="@style/MenuButton"/>
48+
49+
<androidx.appcompat.widget.AppCompatButton
50+
android:text="打开应用市场"
51+
android:onClick="testOpenMarket"
52+
style="@style/MenuButton"/>
53+
54+
<androidx.appcompat.widget.AppCompatButton
55+
android:text="拍照"
56+
android:onClick="testCapture"
57+
style="@style/MenuButton"/>
58+
59+
<androidx.appcompat.widget.AppCompatButton
60+
android:text="获取可以执行分享文本意图的应用的信息"
61+
android:onClick="testGetAppInfo"
62+
style="@style/MenuButton"/>
63+
64+
<androidx.appcompat.widget.AppCompatButton
65+
android:text="Open notification settings"
66+
android:onClick="openNotification"
67+
style="@style/MenuButton"/>
68+
69+
<androidx.appcompat.widget.AppCompatTextView
70+
android:id="@+id/tv_notification_status"
71+
android:layout_width="match_parent"
72+
android:layout_height="wrap_content"/>
73+
74+
</LinearLayout>
75+
76+
</androidx.core.widget.NestedScrollView>

0 commit comments

Comments
 (0)