summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/eip/EipResultBroadcast.java
blob: 92d1338c9bf8cbb7abedd7404193f642d39f1975 (plain)
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
package se.leap.bitmaskclient.eip;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.util.Log;

import static android.content.Intent.CATEGORY_DEFAULT;
import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT;
import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE;
import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_KEY;
import static se.leap.bitmaskclient.Constants.EIP_REQUEST;

public class EipResultBroadcast {
    private static final String TAG = EipResultBroadcast.class.getSimpleName();


    /**
     * send resultCode and resultData to receiver or
     * broadcast the result if no receiver is defined
     *
     * @param action     the action that has been performed
     * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise
     */
    public static void tellToReceiverOrBroadcast(Context context, String action, int resultCode) {
        tellToReceiverOrBroadcast(context, action, resultCode, null, new Bundle());
    }

    /**
     * send resultCode and resultData to receiver or
     * broadcast the result if no receiver is defined
     *
     * @param action     the action that has been performed
     * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise
     * @param resultData other data to broadcast or return to receiver
     */
    public static void tellToReceiverOrBroadcast(Context context, String action, int resultCode, ResultReceiver receiver, Bundle resultData) {
        resultData.putString(EIP_REQUEST, action);
        if (receiver != null) {
            receiver.send(resultCode, resultData);
        } else {
            broadcastEvent(context, resultCode, resultData);
        }
    }

    /**
     * send resultCode and resultData to receiver or
     * broadcast the result if no receiver is defined
     *
     * @param action     the action that has been performed
     * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise
     * @param resultData other data to broadcast or return to receiver
     */
    public static void tellToReceiverOrBroadcast(Context context, String action, int resultCode, Bundle resultData) {
        resultData.putString(EIP_REQUEST, action);
        broadcastEvent(context, resultCode, resultData);
    }



    /**
     * broadcast result
     *
     * @param resultCode RESULT_OK if action was successful RESULT_CANCELED otherwise
     * @param resultData other data to broadcast or return to receiver
     */
    public static void broadcastEvent(Context context, int resultCode, Bundle resultData) {
        Intent intentUpdate = new Intent(BROADCAST_EIP_EVENT);
        intentUpdate.addCategory(CATEGORY_DEFAULT);
        intentUpdate.putExtra(BROADCAST_RESULT_CODE, resultCode);
        intentUpdate.putExtra(BROADCAST_RESULT_KEY, resultData);
        Log.d(TAG, "sending broadcast");
        LocalBroadcastManager.getInstance(context).sendBroadcast(intentUpdate);
    }
}