25 | * Typically uses of this class include testing for corner cases in methods 26 | * that accept input streams and acting as a sentinel value instead of a 27 | * {@code null} input stream. 28 | * 29 | * @version $Id: ClosedInputStream.java 1307459 2012-03-30 15:11:44Z ggregory $ 30 | * @since 1.4 31 | */ 32 | public class ClosedInputStream extends InputStream { 33 | 34 | /** 35 | * A singleton. 36 | */ 37 | public static final ClosedInputStream CLOSED_INPUT_STREAM = new ClosedInputStream(); 38 | 39 | /** 40 | * Returns -1 to indicate that the stream is closed. 41 | * 42 | * @return always -1 43 | */ 44 | @Override 45 | public int read() { 46 | return -1; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/litesuits/common/io/stream/StringBuilderWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.litesuits.common.io.stream; 18 | 19 | import java.io.Serializable; 20 | import java.io.Writer; 21 | 22 | /** 23 | * {@link java.io.Writer} implementation that outputs to a {@link StringBuilder}. 24 | *
25 | * NOTE: This implementation, as an alternative to
26 | * java.io.StringWriter
, provides an un-synchronized
27 | * (i.e. for use in a single thread) implementation for better performance.
28 | * For safe usage with multiple {@link Thread}s then
29 | * java.io.StringWriter
should be used.
30 | *
31 | * @version $Id: StringBuilderWriter.java 1304052 2012-03-22 20:55:29Z ggregory $
32 | * @since 2.0
33 | */
34 | public class StringBuilderWriter extends Writer implements Serializable {
35 |
36 | private final StringBuilder builder;
37 |
38 | /**
39 | * Construct a new {@link StringBuilder} instance with default capacity.
40 | */
41 | public StringBuilderWriter() {
42 | this.builder = new StringBuilder();
43 | }
44 |
45 | /**
46 | * Construct a new {@link StringBuilder} instance with the specified capacity.
47 | *
48 | * @param capacity The initial capacity of the underlying {@link StringBuilder}
49 | */
50 | public StringBuilderWriter(int capacity) {
51 | this.builder = new StringBuilder(capacity);
52 | }
53 |
54 | /**
55 | * Construct a new instance with the specified {@link StringBuilder}.
56 | *
57 | * @param builder The String builder
58 | */
59 | public StringBuilderWriter(StringBuilder builder) {
60 | this.builder = builder != null ? builder : new StringBuilder();
61 | }
62 |
63 | /**
64 | * Append a single character to this Writer.
65 | *
66 | * @param value The character to append
67 | * @return This writer instance
68 | */
69 | @Override
70 | public Writer append(char value) {
71 | builder.append(value);
72 | return this;
73 | }
74 |
75 | /**
76 | * Append a character sequence to this Writer.
77 | *
78 | * @param value The character to append
79 | * @return This writer instance
80 | */
81 | @Override
82 | public Writer append(CharSequence value) {
83 | builder.append(value);
84 | return this;
85 | }
86 |
87 | /**
88 | * Append a portion of a character sequence to the {@link StringBuilder}.
89 | *
90 | * @param value The character to append
91 | * @param start The index of the first character
92 | * @param end The index of the last character + 1
93 | * @return This writer instance
94 | */
95 | @Override
96 | public Writer append(CharSequence value, int start, int end) {
97 | builder.append(value, start, end);
98 | return this;
99 | }
100 |
101 | /**
102 | * Closing this writer has no effect.
103 | */
104 | @Override
105 | public void close() {
106 | }
107 |
108 | /**
109 | * Flushing this writer has no effect.
110 | */
111 | @Override
112 | public void flush() {
113 | }
114 |
115 |
116 | /**
117 | * Write a String to the {@link StringBuilder}.
118 | *
119 | * @param value The value to write
120 | */
121 | @Override
122 | public void write(String value) {
123 | if (value != null) {
124 | builder.append(value);
125 | }
126 | }
127 |
128 | /**
129 | * Write a portion of a character array to the {@link StringBuilder}.
130 | *
131 | * @param value The value to write
132 | * @param offset The index of the first character
133 | * @param length The number of characters to write
134 | */
135 | @Override
136 | public void write(char[] value, int offset, int length) {
137 | if (value != null) {
138 | builder.append(value, offset, length);
139 | }
140 | }
141 |
142 | /**
143 | * Return the underlying builder.
144 | *
145 | * @return The underlying builder
146 | */
147 | public StringBuilder getBuilder() {
148 | return builder;
149 | }
150 |
151 | /**
152 | * Returns {@link StringBuilder#toString()}.
153 | *
154 | * @return The contents of the String builder.
155 | */
156 | @Override
157 | public String toString() {
158 | return builder.toString();
159 | }
160 | }
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/litesuits/common/receiver/PhoneReceiver.java:
--------------------------------------------------------------------------------
1 | package com.litesuits.common.receiver;
2 |
3 | import android.content.BroadcastReceiver;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.IntentFilter;
7 | import android.os.Bundle;
8 | import com.litesuits.android.log.Log;
9 | import com.litesuits.common.assist.Check;
10 |
11 | /**
12 | *
true
传换成小写格式 , false
传换成大写格式
42 | * @return 十六进制char[]
43 | */
44 | public static char[] encodeHex(byte[] data, boolean toLowerCase) {
45 | return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
46 | }
47 |
48 | /**
49 | * 将字节数组转换为十六进制字符数组
50 | *
51 | * @param data
52 | * byte[]
53 | * @param toDigits
54 | * 用于控制输出的char[]
55 | * @return 十六进制char[]
56 | */
57 | protected static char[] encodeHex(byte[] data, char[] toDigits) {
58 | int l = data.length;
59 | char[] out = new char[l << 1];
60 | // two characters form the hex value.
61 | for (int i = 0, j = 0; i < l; i++) {
62 | out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
63 | out[j++] = toDigits[0x0F & data[i]];
64 | }
65 | return out;
66 | }
67 |
68 | /**
69 | * 将字节数组转换为十六进制字符串
70 | *
71 | * @param data
72 | * byte[]
73 | * @return 十六进制String
74 | */
75 | public static String encodeHexStr(byte[] data) {
76 | return encodeHexStr(data, true);
77 | }
78 |
79 | /**
80 | * 将字节数组转换为十六进制字符串
81 | *
82 | * @param data
83 | * byte[]
84 | * @param toLowerCase
85 | * true
传换成小写格式 , false
传换成大写格式
86 | * @return 十六进制String
87 | */
88 | public static String encodeHexStr(byte[] data, boolean toLowerCase) {
89 | return encodeHexStr(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
90 | }
91 |
92 | /**
93 | * 将字节数组转换为十六进制字符串
94 | *
95 | * @param data
96 | * byte[]
97 | * @param toDigits
98 | * 用于控制输出的char[]
99 | * @return 十六进制String
100 | */
101 | protected static String encodeHexStr(byte[] data, char[] toDigits) {
102 | return new String(encodeHex(data, toDigits));
103 | }
104 |
105 | /**
106 | * 将十六进制字符数组转换为字节数组
107 | *
108 | * @param data
109 | * 十六进制char[]
110 | * @return byte[]
111 | * @throws RuntimeException
112 | * 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常
113 | */
114 | public static byte[] decodeHex(char[] data) {
115 |
116 | int len = data.length;
117 |
118 | if ((len & 0x01) != 0) {
119 | throw new RuntimeException("Odd number of characters.");
120 | }
121 |
122 | byte[] out = new byte[len >> 1];
123 |
124 | // two characters form the hex value.
125 | for (int i = 0, j = 0; j < len; i++) {
126 | int f = toDigit(data[j], j) << 4;
127 | j++;
128 | f = f | toDigit(data[j], j);
129 | j++;
130 | out[i] = (byte) (f & 0xFF);
131 | }
132 |
133 | return out;
134 | }
135 |
136 | /**
137 | * 将十六进制字符转换成一个整数
138 | *
139 | * @param ch
140 | * 十六进制char
141 | * @param index
142 | * 十六进制字符在字符数组中的位置
143 | * @return 一个整数
144 | * @throws RuntimeException
145 | * 当ch不是一个合法的十六进制字符时,抛出运行时异常
146 | */
147 | protected static int toDigit(char ch, int index) {
148 | int digit = Character.digit(ch, 16);
149 | if (digit == -1) {
150 | throw new RuntimeException("Illegal hexadecimal character " + ch
151 | + " at index " + index);
152 | }
153 | return digit;
154 | }
155 |
156 | public static void main(String[] args) {
157 | String srcStr = "待转换字符串";
158 | String encodeStr = encodeHexStr(srcStr.getBytes());
159 | String decodeStr = new String(decodeHex(encodeStr.toCharArray()));
160 | System.out.println("转换前:" + srcStr);
161 | System.out.println("转换后:" + encodeStr);
162 | System.out.println("还原后:" + decodeStr);
163 | }
164 |
165 | }
166 |
--------------------------------------------------------------------------------
/app/src/main/java/com/litesuits/common/utils/InputMethodUtils.java:
--------------------------------------------------------------------------------
1 | package com.litesuits.common.utils;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.Activity;
5 | import android.content.Context;
6 | import android.os.Build;
7 | import android.view.View;
8 | import android.view.inputmethod.InputMethodManager;
9 |
10 | /**
11 | * @author MaTianyu(http://litesuits.com) on 2015-06-01
12 | */
13 | @TargetApi(Build.VERSION_CODES.CUPCAKE)
14 | public class InputMethodUtils {
15 |
16 | public static void toggleSoftInput(Context context) {
17 | InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
18 | imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
19 | }
20 |
21 | public static boolean showSoftInput(View view) {
22 | InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
23 | return imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
24 | }
25 |
26 | public static boolean showSoftInput(Activity activity) {
27 | View view = activity.getCurrentFocus();
28 | if (view != null) {
29 | InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(
30 | Context.INPUT_METHOD_SERVICE);
31 | return imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
32 | }
33 | return false;
34 | }
35 |
36 | public static boolean hideSoftInput(View view) {
37 | InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
38 | return imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
39 | }
40 |
41 | public static boolean hideSoftInput(Activity activity) {
42 | if (activity.getCurrentFocus() != null) {
43 | InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
44 | return imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
45 | }
46 | return false;
47 | }
48 |
49 | public static boolean isActive(Context context) {
50 | InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
51 | return imm.isActive();
52 | }
53 |
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/app/src/main/java/com/litesuits/common/utils/MD5Util.java:
--------------------------------------------------------------------------------
1 | package com.litesuits.common.utils;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 | import java.security.MessageDigest;
6 | import java.security.NoSuchAlgorithmException;
7 |
8 | /**
9 | * MS5
10 | */
11 | public class MD5Util {
12 | private static final String TAG = MD5Util.class.getSimpleName();
13 | private static final int STREAM_BUFFER_LENGTH = 1024;
14 |
15 | public static MessageDigest getDigest(final String algorithm) throws NoSuchAlgorithmException {
16 | return MessageDigest.getInstance(algorithm);
17 | }
18 |
19 | public static byte[] md5(String txt) {
20 | return md5(txt.getBytes());
21 | }
22 |
23 | public static byte[] md5(byte[] bytes) {
24 | try {
25 | MessageDigest digest = getDigest("MD5");
26 | digest.update(bytes);
27 | return digest.digest();
28 | } catch (NoSuchAlgorithmException e) {
29 | e.printStackTrace();
30 | }
31 | return null;
32 | }
33 |
34 | public static byte[] md5(InputStream is) throws NoSuchAlgorithmException, IOException {
35 | return updateDigest(getDigest("MD5"), is).digest();
36 | }
37 |
38 | public static MessageDigest updateDigest(final MessageDigest digest, final InputStream data) throws IOException {
39 | final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
40 | int read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
41 |
42 | while (read > -1) {
43 | digest.update(buffer, 0, read);
44 | read = data.read(buffer, 0, STREAM_BUFFER_LENGTH);
45 | }
46 |
47 | return digest;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/java/com/litesuits/common/utils/MemoryUtil.java:
--------------------------------------------------------------------------------
1 | package com.litesuits.common.utils;
2 |
3 | import android.annotation.TargetApi;
4 | import android.app.ActivityManager;
5 | import android.content.Context;
6 | import android.os.Build;
7 | import android.text.format.Formatter;
8 | import com.litesuits.android.log.Log;
9 |
10 | /**
11 | * Get memory info.
12 | *
13 | * @author MaTianyu
14 | * @date 2015-04-19
15 | */
16 | public class MemoryUtil {
17 | private static final String TAG = MemoryUtil.class.getSimpleName();
18 | private static final String MEM_INFO_PATH = "/proc/meminfo";
19 |
20 | /**
21 | * Print memory info. such as:
22 | *
23 | * MemTotal: 1864292 kB
24 | * MemFree: 779064 kB
25 | * Buffers: 4540 kB
26 | * Cached: 185656 kB
27 | * SwapCached: 13160 kB
28 | * Active: 435588 kB
29 | * Inactive: 269312 kB
30 | * Active(anon): 386188 kB
31 | * Inactive(anon): 132576 kB
32 | * Active(file): 49400 kB
33 | * Inactive(file): 136736 kB
34 | * Unevictable: 2420 kB
35 | * Mlocked: 0 kB
36 | * HighTotal: 1437692 kB
37 | * HighFree: 520212 kB
38 | * LowTotal: 426600 kB
39 | * LowFree: 258852 kB
40 | * SwapTotal: 511996 kB
41 | * SwapFree: 171876 kB
42 | * Dirty: 412 kB
43 | * Writeback: 0 kB
44 | * AnonPages: 511924 kB
45 | * Mapped: 152368 kB
46 | * Shmem: 1636 kB
47 | * Slab: 109224 kB
48 | * SReclaimable: 75932 kB
49 | * SUnreclaim: 33292 kB
50 | * KernelStack: 13056 kB
51 | * PageTables: 28032 kB
52 | * NFS_Unstable: 0 kB
53 | * Bounce: 0 kB
54 | * WritebackTmp: 0 kB
55 | * CommitLimit: 1444140 kB
56 | * Committed_AS: 25977748 kB
57 | * VmallocTotal: 458752 kB
58 | * VmallocUsed: 123448 kB
59 | * VmallocChunk: 205828 kB
60 | */
61 | public static String printMemInfo() {
62 | String info = FileUtil.getFileOutputString(MEM_INFO_PATH);
63 | if (Log.isPrint) {
64 | Log.i(TAG, "_______ 内存信息: \n" + info);
65 | }
66 | return info;
67 | }
68 |
69 | /**
70 | * Get memory info of device.
71 | */
72 | @TargetApi(Build.VERSION_CODES.CUPCAKE)
73 | public static ActivityManager.MemoryInfo getMemoryInfo(Context context) {
74 | ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
75 | ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
76 | am.getMemoryInfo(mi);
77 | return mi;
78 | }
79 |
80 | /**
81 | * Print Memory info.
82 | */
83 | @TargetApi(Build.VERSION_CODES.CUPCAKE)
84 | public static ActivityManager.MemoryInfo printMemoryInfo(Context context) {
85 | ActivityManager.MemoryInfo mi = getMemoryInfo(context);
86 | if (Log.isPrint) {
87 | StringBuilder sb = new StringBuilder();
88 | sb.append("_______ Memory : ");
89 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
90 | sb.append("\ntotalMem :").append(mi.totalMem);
91 | }
92 | sb.append("\navailMem :").append(mi.availMem);
93 | sb.append("\nlowMemory :").append(mi.lowMemory);
94 | sb.append("\nthreshold :").append(mi.threshold);
95 | Log.i(TAG, sb.toString());
96 | }
97 | return mi;
98 | }
99 |
100 | /**
101 | * Get available memory info.
102 | */
103 | @TargetApi(Build.VERSION_CODES.CUPCAKE)
104 | public static String getAvailMemory(Context context) {// 获取android当前可用内存大小
105 | ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
106 | ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
107 | am.getMemoryInfo(mi);
108 | // mi.availMem; 当前系统的可用内存
109 | return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化
110 | }
111 |
112 | }
113 |
--------------------------------------------------------------------------------
/app/src/main/java/com/litesuits/common/utils/NotificationUtil.java:
--------------------------------------------------------------------------------
1 | package com.litesuits.common.utils;
2 |
3 | import android.app.Notification;
4 | import android.app.NotificationManager;
5 | import android.app.PendingIntent;
6 | import android.content.ComponentName;
7 | import android.content.Context;
8 | import android.content.Intent;
9 | import android.graphics.Color;
10 | import android.net.Uri;
11 | import android.os.Build;
12 | import android.os.Bundle;
13 | import android.os.Handler;
14 | import android.os.Looper;
15 | import com.litesuits.android.log.Log;
16 |
17 | import java.util.ArrayList;
18 |
19 | /**
20 | * @author MaTianyu
21 | * @date 2014-11-19
22 | */
23 | public class NotificationUtil {
24 | private static int LedID = 0;
25 | private static final String TAG = NotificationUtil.class.getSimpleName();
26 |
27 | public static void notification(Context context, Uri uri,
28 | int icon, String ticker, String title, String msg) {
29 | Log.i(TAG, "notiry uri :" + uri);
30 | // 设置通知的事件消息
31 | Intent intent = new Intent();
32 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
33 | intent.setPackage(context.getPackageName());
34 | }
35 | intent.setData(uri);
36 | notification(context, intent, 0, icon, ticker, title, msg);
37 | }
38 |
39 | public static void notification(Context context, String activityClass, Bundle bundle,
40 | int icon, String ticker, String title, String msg) {
41 | // 设置通知的事件消息
42 | Intent intent = new Intent();
43 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
44 | intent.setPackage(context.getPackageName());
45 | }
46 | intent.putExtras(bundle);
47 | intent.setComponent(new ComponentName(context.getPackageName(), activityClass));
48 | notification(context, intent, 0, icon, ticker, title, msg);
49 | }
50 |
51 | public static void notification(Context context, Intent intent, int id,
52 | int icon, String ticker, String title, String msg) {
53 | PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
54 | notification(context, pendingIntent, id, icon, ticker, title, msg);
55 | }
56 |
57 | public static void notification(Context context, PendingIntent pendingIntent, int id,
58 | int icon, String ticker, String title, String msg) {
59 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
60 | Notification.Builder builder = new Notification.Builder(context);
61 | builder.setSmallIcon(icon);
62 |
63 | builder.setContentTitle(title);
64 | builder.setTicker(ticker);
65 | builder.setContentText(msg);
66 |
67 | builder.setDefaults(Notification.DEFAULT_SOUND);
68 | builder.setLights(0xFFFFFF00, 0, 2000);
69 | builder.setVibrate(new long[]{0, 100, 300});
70 | builder.setAutoCancel(true);
71 | builder.setContentIntent(pendingIntent);
72 | Notification baseNF;
73 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
74 | baseNF = builder.getNotification();
75 | } else {
76 | baseNF = builder.build();
77 | }
78 | //发出状态栏通知
79 | NotificationManager nm = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
80 | nm.notify(id, baseNF);
81 | } else {
82 | // 创建一个NotificationManager的引用
83 | NotificationManager notificationManager = (NotificationManager) context
84 | .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
85 | // 定义Notification的各种属性
86 | Notification notification = new Notification(icon, ticker, System.currentTimeMillis());
87 | notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_SHOW_LIGHTS;
88 | notification.defaults = Notification.DEFAULT_ALL;
89 | notification.ledARGB = Color.GREEN;
90 | notification.ledOnMS = 5000; //闪光时间,毫秒
91 |
92 | notification.tickerText = ticker;
93 | notification.setLatestEventInfo(context, title, msg, pendingIntent);
94 | // 把Notification传递给NotificationManager
95 | notificationManager.notify(id, notification);
96 | }
97 | }
98 |
99 | public static void lightLed(Context context, int colorOx, int durationMS) {
100 | lightLed(context, colorOx, 0, durationMS);
101 | }
102 |
103 | public static void lightLed(Context context, int colorOx, int startOffMS, int durationMS) {
104 | NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
105 | Notification notification = new Notification();
106 | notification.ledARGB = colorOx;
107 | notification.ledOffMS = startOffMS;
108 | notification.ledOnMS = durationMS;
109 | notification.flags = Notification.FLAG_SHOW_LIGHTS;
110 | LedID++;
111 | nm.notify(LedID, notification);
112 | nm.cancel(LedID);
113 | }
114 |
115 | public static void lightLed(final Context context, final int colorOx, final int startOffMS, final int durationMS,
116 | int repeat) {
117 | if (repeat < 1) {
118 | repeat = 1;
119 | }
120 | Handler handler = new Handler(Looper.getMainLooper());
121 | for (int i = 0; i < repeat; i++) {
122 | handler.postDelayed(new Runnable() {
123 | @Override
124 | public void run() {
125 | lightLed(context, colorOx, startOffMS, durationMS);
126 | }
127 | }, (startOffMS + durationMS) * i);
128 | }
129 | }
130 |
131 | public static void lightLed(Context context, ArrayListAll methods requires the caller to hold the permission 8 | * {@link android.Manifest.permission#VIBRATE}. 9 | * 10 | * @author MaTianyu 11 | * @date 2014-11-21 12 | */ 13 | public class VibrateUtil { 14 | 15 | /** 16 | * Vibrate constantly for the specified period of time. 17 | *
This method requires the caller to hold the permission 18 | * {@link android.Manifest.permission#VIBRATE}. 19 | * 20 | * @param milliseconds The number of milliseconds to vibrate. 21 | */ 22 | public static void vibrate(Context context, long milliseconds) { 23 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 24 | vibrator.vibrate(milliseconds); 25 | } 26 | 27 | /** 28 | * Vibrate with a given pattern. 29 | *
30 | *31 | * Pass in an array of ints that are the durations for which to turn on or off 32 | * the vibrator in milliseconds. The first value indicates the number of milliseconds 33 | * to wait before turning the vibrator on. The next value indicates the number of milliseconds 34 | * for which to keep the vibrator on before turning it off. Subsequent values alternate 35 | * between durations in milliseconds to turn the vibrator off or to turn the vibrator on. 36 | *
37 | * To cause the pattern to repeat, pass the index into the pattern array at which 38 | * to start the repeat, or -1 to disable repeating. 39 | *
40 | *This method requires the caller to hold the permission
41 | * {@link android.Manifest.permission#VIBRATE}.
42 | *
43 | * @param pattern an array of longs of times for which to turn the vibrator on or off.
44 | * @param repeat the index into pattern at which to repeat, or -1 if
45 | * you don't want to repeat.
46 | */
47 | public static void vibrate(Context context, long[] pattern, int repeat) {
48 | Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
49 | vibrator.vibrate(pattern, repeat);
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |