lmw
2023-06-16 03972ad1d3ce6ffe0be0395c0a4d5dcb4474031f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.luck.picture.lib.broadcast;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
 
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
/**
 * @author:luck
 * @date:2019-11-20 13:45
 * @describe:本地广播
 */
public class BroadcastManager {
    private static final String TAG = BroadcastManager.class.getSimpleName();
    private LocalBroadcastManager localBroadcastManager;
 
    private Intent intent;
    private String action;
 
    public static BroadcastManager getInstance(Context ctx) {
        BroadcastManager broadcastManager = new BroadcastManager();
        broadcastManager.localBroadcastManager = LocalBroadcastManager.getInstance(ctx.getApplicationContext());
        return broadcastManager;
    }
 
 
    public BroadcastManager intent(Intent intent) {
        this.intent = intent;
        return this;
    }
 
 
    public BroadcastManager action(String action) {
        this.action = action;
        return this;
    }
 
    public BroadcastManager extras(Bundle bundle) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
        intent.putExtras(bundle);
        return this;
    }
 
 
    public BroadcastManager put(String key, ArrayList<? extends Parcelable> value) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
 
        intent.putExtra(key, value);
        return this;
    }
 
    public BroadcastManager put(String key, Parcelable[] value) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
 
        intent.putExtra(key, value);
        return this;
    }
 
 
    public BroadcastManager put(String key, Parcelable value) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
 
        intent.putExtra(key, value);
        return this;
    }
 
    public BroadcastManager put(String key, float value) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
 
        intent.putExtra(key, value);
        return this;
    }
 
    public BroadcastManager put(String key, double value) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
 
        intent.putExtra(key, value);
        return this;
    }
 
    public BroadcastManager put(String key, long value) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
 
        intent.putExtra(key, value);
        return this;
    }
 
    public BroadcastManager put(String key, boolean value) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
 
        intent.putExtra(key, value);
        return this;
    }
 
    public BroadcastManager put(String key, int value) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
 
        intent.putExtra(key, value);
        return this;
    }
 
 
    public BroadcastManager put(String key, String str) {
        createIntent();
 
        if (intent == null) {
            Log.e(TAG, "intent create failed");
            return this;
        }
 
        intent.putExtra(key, str);
        return this;
    }
 
    private void createIntent() {
        if (intent == null) {
            Log.d(TAG, "intent is not created");
        }
 
        if (intent == null) {
            if (!TextUtils.isEmpty(action)) {
                intent = new Intent(action);
            }
            Log.d(TAG, "intent created with action");
        }
    }
 
 
    public void broadcast() {
 
        createIntent();
 
        if (intent == null) {
            return;
        }
 
        if (action == null) {
            return;
        }
 
        intent.setAction(action);
 
        if (null != localBroadcastManager) {
            localBroadcastManager.sendBroadcast(intent);
        }
    }
 
    public void registerReceiver(BroadcastReceiver br, List<String> actions) {
        if (null == br || null == actions) {
            return;
        }
        IntentFilter iFilter = new IntentFilter();
        if (actions != null) {
            for (String action : actions) {
                iFilter.addAction(action);
            }
        }
        if (null != localBroadcastManager) {
            localBroadcastManager.registerReceiver(br, iFilter);
        }
    }
 
 
    public void registerReceiver(BroadcastReceiver br, String... actions) {
        if (actions == null || actions.length <= 0) {
            return;
        }
        registerReceiver(br, Arrays.asList(actions));
    }
 
 
    /**
     * @param br
     */
    public void unregisterReceiver(BroadcastReceiver br) {
        if (null == br) {
            return;
        }
 
        try {
            localBroadcastManager.unregisterReceiver(br);
        } catch (Exception e) {
 
        }
    }
 
    /**
     * @param br
     * @param actions 至少传入一个
     */
    public void unregisterReceiver(BroadcastReceiver br, @NonNull String... actions) {
        unregisterReceiver(br);
    }
}