summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/ProviderAPICommand.java
blob: 65d01b2266177e5cd545b6f498d5d540c4b31316 (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
78
79
package se.leap.bitmaskclient;

import android.content.*;
import android.os.*;

import org.jetbrains.annotations.*;

public class ProviderAPICommand {
    private Context context;

    private String action;
    private Bundle parameters;
    private ResultReceiver resultReceiver;
    private Provider provider;

    private ProviderAPICommand(@NotNull Context context, @NotNull String action, @NotNull Provider provider, ResultReceiver resultReceiver) {
        this(context, action, Bundle.EMPTY, provider, resultReceiver);
    }
    private ProviderAPICommand(@NotNull Context context, @NotNull String action, @NotNull Provider provider) {
        this(context, action, Bundle.EMPTY, provider);
    }

    private ProviderAPICommand(@NotNull Context context, @NotNull String action, @NotNull Bundle parameters, @NotNull Provider provider) {
        this(context, action, parameters, provider, null);
    }

    private ProviderAPICommand(@NotNull Context context, @NotNull String action, @NotNull Bundle parameters, @NotNull Provider provider, @Nullable ResultReceiver resultReceiver) {
        super();
        this.context = context;
        this.action = action;
        this.parameters = parameters;
        this.resultReceiver = resultReceiver;
        this.provider = provider;
    }

    private boolean isInitialized() {
        return context != null;
    }

    private void execute() {
        if (isInitialized()) {
            Intent intent = setUpIntent();
            context.startService(intent);
        }
    }

    private Intent setUpIntent() {
        Intent command = new Intent(context, ProviderAPI.class);

        command.setAction(action);
        command.putExtra(ProviderAPI.PARAMETERS, parameters);
        if (resultReceiver != null) {
            command.putExtra(ProviderAPI.RECEIVER_KEY, resultReceiver);
        }
        command.putExtra(Constants.PROVIDER_KEY, provider);

        return command;
    }

    public static void execute(Context context, String action, Provider provider) {
        ProviderAPICommand command = new ProviderAPICommand(context, action, provider);
        command.execute();
    }

    public static void execute(Context context, String action, Bundle parameters, Provider provider) {
        ProviderAPICommand command = new ProviderAPICommand(context, action, parameters, provider);
        command.execute();
    }

    public static void execute(Context context, String action, Bundle parameters, Provider provider, ResultReceiver resultReceiver) {
        ProviderAPICommand command = new ProviderAPICommand(context, action, parameters, provider, resultReceiver);
        command.execute();
    }

    public static void execute(Context context, String action, Provider provider, ResultReceiver resultReceiver) {
        ProviderAPICommand command = new ProviderAPICommand(context, action, provider, resultReceiver);
        command.execute();
    }
}