summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/util/SecureMessagingHelper.java
blob: 8dfd27612b3e035cc42da772d59486a42ce5f408 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.android.messaging.util;

import android.content.Context;
import android.os.AsyncTask;

import com.android.messaging.datamodel.data.ConversationParticipantsData;
import com.android.messaging.datamodel.data.ParticipantData;

import org.whispersystems.whisperpush.WhisperPush;
import org.whispersystems.whisperpush.directory.Directory;

import java.util.ArrayList;
import java.util.List;

import static org.whispersystems.whisperpush.directory.Directory.STATE_ALL_CONTACTS_SECURE;
import static org.whispersystems.whisperpush.directory.Directory.STATE_ALL_CONTACTS_UNSECURE;
import static org.whispersystems.whisperpush.directory.Directory.STATE_CONTACTS_MIXED;

/**
 * Provides API for secure messaging (currently - via WhisperPush)
 */
public class SecureMessagingHelper {
    private static final int STATE_UNKNOWN = 0;

    private Context mContext;
    private WhisperPush mWhisperPush;
    private SecurityMessagingCallback mCallback;
    private int mParticipantsSecurityState = STATE_UNKNOWN;
    private boolean mIsUpdatingParticipantsSecurity;

    public interface SecurityMessagingCallback {
        void onParticipantsSecurityUpdated();
        ConversationParticipantsData getParticipantsData();
    }

    public SecureMessagingHelper(Context context, SecurityMessagingCallback callback) {
        mContext = context;
        mWhisperPush = WhisperPush.getInstance(context);
        mCallback = callback;
    }

    public void checkIfAllParticipantsSecuredAsync() {
        mIsUpdatingParticipantsSecurity = true;
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                checkIfAllParticipantsSecured();
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                mCallback.onParticipantsSecurityUpdated();
                mIsUpdatingParticipantsSecurity = false;
            }
        }.execute();

    }

    public void checkIfAllParticipantsSecured() {
        if (mWhisperPush.isSecureMessagingActive()) {
            List<String> participantsSendDestinations = getSendDestinations();

            if (participantsSendDestinations != null) {
                mParticipantsSecurityState
                        = Directory.getInstance(mContext).isAllActiveNumbers(participantsSendDestinations);
            } else {
                mParticipantsSecurityState = STATE_UNKNOWN;
            }
        } else {
            mParticipantsSecurityState = STATE_UNKNOWN;
        }
    }

    private List<String> getSendDestinations() {
        ConversationParticipantsData participantsData = mCallback.getParticipantsData();

        if (participantsData != null) {
            List<String> participantsSendDestinations = new ArrayList<String>();

            for (ParticipantData participant : participantsData.getParticipantListExcludingSelf()){
                participantsSendDestinations.add(participant.getSendDestination());
            }

            return participantsSendDestinations;
        } else {
            return null;
        }
    }

    public boolean isUpdatingParticipantsSecurity() {
        return mIsUpdatingParticipantsSecurity;
    }

    public boolean isAllParticipantsSecured() {
        return mParticipantsSecurityState == STATE_ALL_CONTACTS_SECURE;
    }

    public boolean isParticipantsMixed() {
        return mParticipantsSecurityState == STATE_CONTACTS_MIXED;
    }
}