Skip to content

Commit 6117368

Browse files
committed
see 02/20 log
1 parent 7f1ddcb commit 6117368

File tree

4 files changed

+149
-47
lines changed

4 files changed

+149
-47
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.blankj.utilcode.util;
2+
3+
import android.app.Notification;
4+
import android.app.NotificationManager;
5+
import android.app.PendingIntent;
6+
import android.content.Context;
7+
import android.content.Intent;
8+
import android.support.annotation.Nullable;
9+
import android.support.v4.app.NotificationCompat;
10+
import android.support.v4.app.NotificationManagerCompat;
11+
12+
/**
13+
* <pre>
14+
* author: blankj
15+
* blog : http://blankj.com
16+
* time : 2019/02/20
17+
* desc : utils about notification
18+
* </pre>
19+
*/
20+
public class NotificationUtils {
21+
22+
private NotificationUtils() {
23+
throw new UnsupportedOperationException("u can't instantiate me...");
24+
}
25+
26+
public static void create(Context context, int id, Intent intent, int smallIcon, String contentTitle, String contentText) {
27+
NotificationManager manager =
28+
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
29+
30+
// Intent para disparar o broadcast
31+
PendingIntent p = PendingIntent.getActivity(Utils.getApp(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
32+
33+
// Cria a notification
34+
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
35+
.setContentIntent(p)
36+
.setContentTitle(contentTitle)
37+
.setContentText(contentText)
38+
.setSmallIcon(smallIcon)
39+
.setAutoCancel(true);
40+
41+
// Dispara a notification
42+
Notification n = builder.build();
43+
manager.notify(id, n);
44+
}
45+
46+
public static void createStackNotification(Context context, int id, String groupId, Intent intent, int smallIcon, String contentTitle, String contentText) {
47+
NotificationManager manager =
48+
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
49+
50+
// Intent para disparar o broadcast
51+
PendingIntent p = intent != null ? PendingIntent.getActivity(Utils.getApp(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) : null;
52+
53+
// Cria a notification
54+
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
55+
.setContentIntent(p)
56+
.setContentTitle(contentTitle)
57+
.setContentText(contentText)
58+
.setSmallIcon(smallIcon)
59+
.setGroup(groupId)
60+
.setAutoCancel(true);
61+
62+
// Dispara a notification
63+
Notification n = builder.build();
64+
manager.notify(id, n);
65+
}
66+
67+
// Notificação simples sem abrir intent (usada para alertas, ex: no wear)
68+
public static void create(int smallIcon, String contentTitle, String contentText) {
69+
NotificationManager manager =
70+
(NotificationManager) Utils.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
71+
72+
// Cria a notification
73+
NotificationCompat.Builder builder = new NotificationCompat.Builder(Utils.getApp())
74+
.setContentTitle(contentTitle)
75+
.setContentText(contentText)
76+
.setSmallIcon(smallIcon)
77+
.setAutoCancel(true);
78+
79+
// Dispara a notification
80+
Notification n = builder.build();
81+
manager.notify(0, n);
82+
}
83+
84+
public static void cancel(@Nullable String tag, final int id) {
85+
NotificationManagerCompat.from(Utils.getApp()).cancel(tag, id);
86+
}
87+
88+
public static void cancel(final int id) {
89+
NotificationManagerCompat.from(Utils.getApp()).cancel(id);
90+
}
91+
92+
public static void cancelAll() {
93+
NotificationManagerCompat.from(Utils.getApp()).cancelAll();
94+
}
95+
}

utilcode/lib/src/main/java/com/blankj/utilcode/util/ThreadUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,6 @@ private synchronized static ExecutorService getPoolByTypeAndPriority(final int t
920920
private static ExecutorService createPoolByTypeAndPriority(final int type, final int priority) {
921921
switch (type) {
922922
case TYPE_SINGLE:
923-
System.out.println("hehe");
924923
return Executors.newSingleThreadExecutor(
925924
new UtilsThreadFactory("single", priority)
926925
);

utilcode/lib/src/main/java/com/blankj/utilcode/util/http/HttpUtils.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import java.io.InputStream;
1313
import java.io.OutputStream;
1414
import java.net.HttpURLConnection;
15-
import java.net.Proxy;
16-
import java.net.URL;
1715
import java.security.cert.X509Certificate;
1816
import java.util.Deque;
1917
import java.util.LinkedList;
@@ -71,8 +69,10 @@ public boolean verify(String hostname, SSLSession session) {
7169

7270
private static final Config CONFIG = new Config();
7371

74-
private HttpUtils() {
75-
throw new UnsupportedOperationException("u can't instantiate me...");
72+
73+
74+
private HttpUtils(Config config) {
75+
7676
}
7777

7878
public static void call(@NonNull final Request request, @NonNull final ResponseCallback callback) {
@@ -81,7 +81,7 @@ public static void call(@NonNull final Request request, @NonNull final ResponseC
8181
}
8282

8383
private static HttpURLConnection getConnection(final Request request) throws IOException {
84-
HttpURLConnection conn = (HttpURLConnection) request.mURL.openConnection(new Proxy());
84+
HttpURLConnection conn = (HttpURLConnection) request.mURL.openConnection();
8585
if (conn instanceof HttpsURLConnection) {
8686
HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
8787
trustAllHosts(httpsConn);
@@ -225,18 +225,18 @@ public static class Dispatcher {
225225
private final Deque<Call> readyCalls = new LinkedList<>();
226226
private final Deque<Call> runningCalls = new LinkedList<>();
227227

228-
synchronized void enqueue(Call call) {
229-
// 不超过最大请求数并且不超过 host 最大请求数
230-
if (runningCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
231-
// 添加到运行中的异步请求队列
232-
runningAsyncCalls.add(call);
233-
// 添加到线程池中运行
234-
executorService().execute(call);
235-
} else {
236-
// 添加到就绪的异步请求队列
237-
readyAsyncCalls.add(call);
238-
}
239-
}
228+
// synchronized void enqueue(Call call) {
229+
// // 不超过最大请求数并且不超过 host 最大请求数
230+
// if (runningCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
231+
// // 添加到运行中的异步请求队列
232+
// runningAsyncCalls.add(call);
233+
// // 添加到线程池中运行
234+
// executorService().execute(call);
235+
// } else {
236+
// // 添加到就绪的异步请求队列
237+
// readyAsyncCalls.add(call);
238+
// }
239+
// }
240240
}
241241

242242
static class Call implements Runnable {

utilcode/lib/src/test/java/com/blankj/utilcode/util/BaseTest.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import org.robolectric.annotation.Config;
99
import org.robolectric.shadows.ShadowLog;
1010

11+
import java.util.Scanner;
12+
import java.util.concurrent.CountDownLatch;
13+
import java.util.concurrent.ExecutorService;
14+
1115
/**
1216
* <pre>
1317
* author: Blankj
@@ -29,36 +33,40 @@ public BaseTest() {
2933

3034
@Test
3135
public void test() throws Exception {
32-
// final CountDownLatch countDownLatch = new CountDownLatch(1);
33-
// final Scanner scanner = new Scanner(System.in);
34-
// ExecutorService singlePool = ThreadUtils.getSinglePool();
35-
// singlePool.execute(new Runnable() {
36-
// @Override
37-
// public void run() {
38-
// for (int i = 0; i < 1000; i++) {
39-
// if (Thread.currentThread().isInterrupted()) {
40-
// break;
41-
// }
42-
// System.out.println(i);
43-
// try {
44-
// Thread.sleep(10);
45-
// } catch (InterruptedException e) {
46-
// e.printStackTrace();
47-
// }
48-
// }
49-
// System.out.println("scanner start");
50-
// scanner.nextLine();
51-
// System.out.println("scanner end");
52-
//
53-
// }
54-
// });
55-
// Thread.sleep(200);
36+
final CountDownLatch countDownLatch = new CountDownLatch(1);
37+
final Scanner scanner = new Scanner(System.in);
38+
ExecutorService singlePool = ThreadUtils.getSinglePool();
39+
final Thread[] thread = new Thread[1];
40+
singlePool.execute(new Runnable() {
41+
@Override
42+
public void run() {
43+
thread[0] = Thread.currentThread();
44+
for (int i = 0; i < 1000; i++) {
45+
if (Thread.currentThread().isInterrupted()) {
46+
break;
47+
}
48+
System.out.println(i);
49+
try {
50+
Thread.sleep(100);
51+
} catch (InterruptedException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
System.out.println("scanner start");
56+
scanner.nextLine();
57+
System.out.println("scanner end");
58+
59+
}
60+
});
61+
Thread.sleep(200);
62+
thread[0].interrupt();
63+
System.out.println("haha");
5664
// singlePool.shutdownNow();
57-
//
58-
//
59-
// countDownLatch.await();
6065

6166

67+
countDownLatch.await();
68+
69+
//
6270
// final CountDownLatch countDownLatch = new CountDownLatch(1);
6371
// final Scanner s = new Scanner(System.in);
6472
// final ThreadUtils.SimpleTask<Void> task = new ThreadUtils.SimpleTask<Void>() {
@@ -75,7 +83,7 @@ public void test() throws Exception {
7583
// }
7684
//
7785
// @Override
78-
// public void onResponse(@Nullable Void result) {
86+
// public void onSuccess(@Nullable Void result) {
7987
// countDownLatch.countDown();
8088
// }
8189
//
@@ -99,7 +107,7 @@ public void test() throws Exception {
99107
// }
100108
//
101109
// @Override
102-
// public void onResponse(@Nullable Void result) {
110+
// public void onSuccess(@Nullable Void result) {
103111
// countDownLatch.countDown();
104112
// }
105113
//

0 commit comments

Comments
 (0)