|
3 | 3 | import android.app.Activity;
|
4 | 4 | import android.app.Dialog;
|
5 | 5 | import android.content.Context;
|
6 |
| -import android.content.DialogInterface; |
7 | 6 |
|
8 |
| -import java.util.ArrayList; |
9 | 7 | import java.util.HashMap;
|
| 8 | +import java.util.HashSet; |
10 | 9 | import java.util.Iterator;
|
11 |
| -import java.util.List; |
12 |
| -import java.util.ListIterator; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Set; |
13 | 12 |
|
14 | 13 | /**
|
15 | 14 | * Created by hss on 2018/3/24.
|
16 | 15 | */
|
17 | 16 |
|
18 | 17 | public class DialogsMaintainer {
|
19 | 18 |
|
20 |
| - private static HashMap<Activity,List<Dialog>> dialogsOfActivity = new HashMap<>(); |
21 |
| - private static List<Dialog> loadingDialogs = new ArrayList<>(); |
| 19 | + private static HashMap<Activity, Set<Dialog>> dialogsOfActivity = new HashMap<>(); |
| 20 | + private static HashMap<Activity, Set<Dialog>> loadingDialogs = new HashMap<>(); |
22 | 21 |
|
23 | 22 |
|
| 23 | + public static void addLoadingDialog(Context context, Dialog dialog) { |
24 | 24 |
|
25 |
| - public static void addLoadingDialog(Dialog dialog){ |
26 |
| - if(dialog.getOwnerActivity() instanceof Activity){ |
27 |
| - loadingDialogs.add(dialog); |
| 25 | + if (!(context instanceof Activity)) { |
| 26 | + return; |
| 27 | + } |
| 28 | + Activity activity = (Activity) context; |
| 29 | + |
| 30 | + Set<Dialog> dialogs = null; |
| 31 | + if (loadingDialogs.containsKey(activity)) { |
| 32 | + dialogs = loadingDialogs.get(activity); |
| 33 | + } |
| 34 | + if (dialogs == null) { |
| 35 | + dialogs = new HashSet<>(); |
| 36 | + loadingDialogs.put(activity, dialogs); |
28 | 37 | }
|
| 38 | + dialogs.add(dialog); |
| 39 | + |
29 | 40 |
|
30 | 41 | }
|
31 | 42 |
|
32 |
| - public static void dismissLoading(Activity activity){ |
33 |
| - if(activity ==null){ |
| 43 | + public static void dismissLoading(Activity activity) { |
| 44 | + |
| 45 | + if (activity == null) { |
34 | 46 | return;
|
35 | 47 | }
|
36 |
| - ListIterator<Dialog> iterator = loadingDialogs.listIterator(); |
37 |
| - while (iterator.hasNext()){ |
38 |
| - Dialog dialog = iterator.next(); |
39 |
| - if(dialog.getOwnerActivity().equals(activity)){ |
40 |
| - dialog.dismiss(); |
41 |
| - iterator.remove(); |
42 |
| - removeWhenDismiss(dialog); |
| 48 | + if (!loadingDialogs.containsKey(activity)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + Set<Dialog> dialogSet = loadingDialogs.get(activity); |
| 52 | + for (Dialog dialog : dialogSet) { |
| 53 | + dialog.dismiss(); |
| 54 | + //在callback内部自动会去移除在dialogsOfActivity的引用 |
| 55 | + } |
| 56 | + loadingDialogs.remove(activity); |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + public static void dismissLoading(Dialog dialog) { |
| 61 | + Iterator<Map.Entry<Activity, Set<Dialog>>> entryIterator = loadingDialogs.entrySet().iterator(); |
| 62 | + while (entryIterator.hasNext()) { |
| 63 | + Map.Entry<Activity, Set<Dialog>> entry = entryIterator.next(); |
| 64 | + Set<Dialog> dialogInterfaces = entry.getValue(); |
| 65 | + if (dialogInterfaces == null || dialogInterfaces.isEmpty()) { |
| 66 | + entryIterator.remove(); |
| 67 | + return; |
| 68 | + } |
| 69 | + Iterator<Dialog> iterator = dialogInterfaces.iterator(); |
| 70 | + while (iterator.hasNext()) { |
| 71 | + Dialog dialog0 = iterator.next(); |
| 72 | + if (dialog.equals(dialog0)) { |
| 73 | + iterator.remove(); |
| 74 | + } |
| 75 | + } |
| 76 | + if (dialogInterfaces == null || dialogInterfaces.isEmpty()) { |
| 77 | + entryIterator.remove(); |
| 78 | + return; |
43 | 79 | }
|
44 | 80 | }
|
45 | 81 | }
|
46 | 82 |
|
47 |
| - public static void addWhenShow(Dialog dialog){ |
| 83 | + public static void addWhenShow(Context context, Dialog dialog) { |
48 | 84 |
|
49 |
| - Activity activity = dialog.getOwnerActivity(); |
50 |
| - if(activity ==null){ |
| 85 | + if (!(context instanceof Activity)) { |
51 | 86 | return;
|
52 | 87 | }
|
53 |
| - List<Dialog> dialogs = null; |
54 |
| - if(dialogsOfActivity.containsKey(activity)){ |
| 88 | + |
| 89 | + Activity activity = (Activity) context; |
| 90 | + |
| 91 | + Set<Dialog> dialogs = null; |
| 92 | + if (dialogsOfActivity.containsKey(activity)) { |
55 | 93 | dialogs = dialogsOfActivity.get(activity);
|
56 | 94 | }
|
57 |
| - if(dialogs == null){ |
58 |
| - dialogs = new ArrayList<>(); |
59 |
| - dialogsOfActivity.put(activity,dialogs); |
| 95 | + if (dialogs == null) { |
| 96 | + dialogs = new HashSet<>(); |
| 97 | + dialogsOfActivity.put(activity, dialogs); |
60 | 98 | }
|
61 | 99 | dialogs.add(dialog);
|
| 100 | + |
62 | 101 | }
|
63 | 102 |
|
64 |
| - public static void removeWhenDismiss(Dialog dialog){ |
65 |
| - Activity activity = dialog.getOwnerActivity(); |
66 |
| - if(activity ==null){ |
67 |
| - return; |
68 |
| - } |
69 |
| - if(!dialogsOfActivity.containsKey(activity)){ |
70 |
| - return; |
71 |
| - } |
72 |
| - List<Dialog> dialogInterfaces = dialogsOfActivity.get(activity); |
73 |
| - if(dialogInterfaces==null || dialogInterfaces.isEmpty()){ |
74 |
| - return; |
75 |
| - } |
76 |
| - ListIterator<Dialog> iterator = dialogInterfaces.listIterator(); |
77 |
| - while (iterator.hasNext()){ |
78 |
| - Dialog dialog0 = iterator.next(); |
79 |
| - if(dialog.equals(dialog0)){ |
80 |
| - iterator.remove(); |
| 103 | + public static void removeWhenDismiss(Dialog dialog) { |
| 104 | + |
| 105 | + try { |
| 106 | + Iterator<Map.Entry<Activity, Set<Dialog>>> entryIterator = dialogsOfActivity.entrySet().iterator(); |
| 107 | + while (entryIterator.hasNext()) { |
| 108 | + Map.Entry<Activity, Set<Dialog>> entry = entryIterator.next(); |
| 109 | + Set<Dialog> dialogInterfaces = entry.getValue(); |
| 110 | + if (dialogInterfaces == null || dialogInterfaces.isEmpty()) { |
| 111 | + entryIterator.remove(); |
| 112 | + return; |
| 113 | + } |
| 114 | + Iterator<Dialog> iterator = dialogInterfaces.iterator(); |
| 115 | + while (iterator.hasNext()) { |
| 116 | + Dialog dialog0 = iterator.next(); |
| 117 | + if (dialog.equals(dialog0)) { |
| 118 | + iterator.remove(); |
| 119 | + } |
| 120 | + } |
| 121 | + if (dialogInterfaces == null || dialogInterfaces.isEmpty()) { |
| 122 | + entryIterator.remove(); |
| 123 | + return; |
| 124 | + } |
81 | 125 | }
|
| 126 | + } catch (Exception e) { |
| 127 | + e.printStackTrace(); |
82 | 128 | }
|
83 | 129 |
|
| 130 | + |
84 | 131 | }
|
85 | 132 |
|
86 | 133 | /**
|
87 | 134 | * 在这里移除已经dismiss的dialog引用
|
| 135 | + * |
88 | 136 | * @param activity
|
89 | 137 | */
|
90 |
| - public static void onPause(Activity activity){ |
91 |
| - if(!dialogsOfActivity.containsKey(activity)){ |
| 138 | + public static void onPause(Activity activity) { |
| 139 | + if (!dialogsOfActivity.containsKey(activity)) { |
92 | 140 | return;
|
93 | 141 | }
|
94 |
| - List<Dialog> dialogInterfaces = dialogsOfActivity.get(activity); |
95 |
| - if(dialogInterfaces==null || dialogInterfaces.isEmpty()){ |
| 142 | + Set<Dialog> dialogInterfaces = dialogsOfActivity.get(activity); |
| 143 | + if (dialogInterfaces == null || dialogInterfaces.isEmpty()) { |
96 | 144 | dialogInterfaces.remove(activity);
|
97 | 145 | return;
|
98 | 146 | }
|
99 |
| - ListIterator<Dialog> iterator = dialogInterfaces.listIterator(); |
100 |
| - while (iterator.hasNext()){ |
| 147 | + Iterator<Dialog> iterator = dialogInterfaces.iterator(); |
| 148 | + while (iterator.hasNext()) { |
101 | 149 | Dialog dialog = iterator.next();
|
102 |
| - if(!dialog.isShowing()){ |
| 150 | + if (!dialog.isShowing()) { |
103 | 151 | iterator.remove();
|
104 | 152 | }
|
105 | 153 | }
|
| 154 | + if (dialogInterfaces == null || dialogInterfaces.isEmpty()) { |
| 155 | + dialogInterfaces.remove(activity); |
| 156 | + return; |
| 157 | + } |
106 | 158 |
|
107 | 159 | }
|
108 | 160 |
|
109 |
| - public static void onDestory(Activity activity){ |
110 |
| - if(!dialogsOfActivity.containsKey(activity)){ |
| 161 | + public static void onDestory(Activity activity) { |
| 162 | + |
| 163 | + if (!dialogsOfActivity.containsKey(activity)) { |
111 | 164 | return;
|
112 | 165 | }
|
113 |
| - List<Dialog> dialogInterfaces = dialogsOfActivity.get(activity); |
114 |
| - if(dialogInterfaces==null || dialogInterfaces.isEmpty()){ |
| 166 | + Set<Dialog> dialogInterfaces = dialogsOfActivity.get(activity); |
| 167 | + if (dialogInterfaces == null || dialogInterfaces.isEmpty()) { |
115 | 168 | dialogInterfaces.remove(activity);
|
116 | 169 | return;
|
117 | 170 | }
|
118 | 171 | for (Dialog dialog : dialogInterfaces) {
|
119 |
| - if(dialog!=null){ |
| 172 | + if (dialog != null && dialog.isShowing()) { |
120 | 173 | dialog.dismiss();
|
121 | 174 | }
|
122 | 175 | }
|
123 | 176 | dialogInterfaces.remove(activity);
|
| 177 | + |
124 | 178 | }
|
125 | 179 | }
|
0 commit comments