summaryrefslogtreecommitdiffstats
path: root/quickReader/src/org/lineageos/quickreader/ScannerIntentHelper.java
blob: c8fa500c18119def606deaf551a2370ca1dccab5 (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
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
/*
 * Copyright (C) 2017 The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.lineageos.quickreader;

import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.codeaurora.snapcam.R;

class ScannerIntentHelper {
    private static final String EVENT_SUMMARY = "SUMMARY:";
    private static final String EVENT_DESCRIPTION = "DESCRIPTION:";
    private static final String EVENT_START = "DTSTART:";
    private static final String EVENT_END = "DTEND:";
    private static final String EVENT_LOCATION = "LOCATION:";
    private static final String EVENT_DATE_FORMAT = "yyyyMMdd";

    private static final String SMS_URI = "smsto:";
    private static final String SMS_BODY = "sms_body";

    private static final String CONTACT_NAME = "N:";
    private static final String CONTACT_ORG = "ORG:";
    private static final String CONTACT_TITLE = "TITLE:";
    private static final String CONTACT_TEL = "TEL:";
    private static final String CONTACT_EMAIL = "EMAIL:";
    private static final String CONTACT_NOTE = "NOTE:";

    private static final String MEBKM_URL = "URL:";

    private static ScannerIntentHelper INSTANCE;

    private Intent mIntent;

    private String mScannedText;

    private ScannerIntentHelper() {
    }

    static ScannerIntentHelper getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new ScannerIntentHelper();
        }

        return INSTANCE;
    }

    void run(Context context) {
        if (mIntent == null) {
            showScannedText(context);
        } else {
            runIntent(context);
        }
    }

    void reset() {
        mIntent = null;
        mScannedText = null;
    }

    boolean isValid() {
        return mIntent != null || !TextUtils.isEmpty(mScannedText);
    }

    void setUriIntent(String uri) {
        mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
    }

    void setCalendarIntent(String text) {
        String[] data = text.split("\n");
        String summary = "";
        String description = "";
        String location = "";
        Date start = new Date();
        Date end = new Date();

        for (String item : data) {
            if (item.startsWith(EVENT_SUMMARY)) {
                summary = item.replace(EVENT_SUMMARY, "");
            } else if (item.startsWith(EVENT_DESCRIPTION)) {
                summary = item.replace(EVENT_DESCRIPTION, "");
            } else if (item.startsWith(EVENT_LOCATION)) {
                location = item.replace(EVENT_LOCATION, "");
            } else if (item.startsWith(EVENT_START)) {
                start = parseDate(item.replace(EVENT_START, ""));
            } else if (item.startsWith(EVENT_END)) {
                start = parseDate(item.replace(EVENT_END, ""));
            }
        }

        mIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
        mIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start)
                .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end)
                .putExtra(CalendarContract.Events.TITLE, summary)
                .putExtra(CalendarContract.Events.DESCRIPTION, description)
                .putExtra(CalendarContract.Events.EVENT_LOCATION, location);
    }

    void setSMSIntent(String text) {
        String[] data = text.split(":");

        mIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse(SMS_URI + data[1]));
        if (data.length > 2) {
            mIntent.putExtra(SMS_BODY, data[2]);
        }
    }

    void setContactIntent(String text, boolean isVCard) {
        String[] data = text.split(isVCard ? "\n" : ";");
        String name = "";
        String org = "";
        String title = "";
        String telephone = "";
        String email = "";
        String notes = "";


        for (String item : data) {
            if (item.startsWith(CONTACT_NAME)) {
                name = item.replace(CONTACT_NAME, "");
            } else if (item.startsWith(CONTACT_ORG)) {
                org = item.replace(CONTACT_ORG, "");
            } else if (item.startsWith(CONTACT_TITLE)) {
                title = item.replace(CONTACT_TITLE, "");
            } else if (item.startsWith(CONTACT_TEL)) {
                telephone = item.replace(CONTACT_TEL, "");
            } else if (item.startsWith(CONTACT_EMAIL)) {
                email = item.replace(CONTACT_EMAIL, "");
            } else if (item.startsWith(CONTACT_NOTE)) {
                notes = item.replace(CONTACT_NOTE, "");
            }
        }

        mIntent = new Intent(ContactsContract.Intents.Insert.ACTION);
        mIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE)
                .putExtra(ContactsContract.Intents.Insert.NAME, name)
                .putExtra(ContactsContract.Intents.Insert.COMPANY, org)
                .putExtra(ContactsContract.Intents.Insert.JOB_TITLE, title)
                .putExtra(ContactsContract.Intents.Insert.PHONE, telephone)
                .putExtra(ContactsContract.Intents.Insert.PHONE_TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                .putExtra(ContactsContract.Intents.Insert.EMAIL, email)
                .putExtra(ContactsContract.Intents.Insert.EMAIL_TYPE,
                        ContactsContract.CommonDataKinds.Email.TYPE_WORK)
                .putExtra(ContactsContract.Intents.Insert.NOTES, notes);
    }

    void setMeBkmUrl(String text) {
        String[] data = text.split(":");

        for (String item : data) {
            if (!item.startsWith(MEBKM_URL)) {
                continue;
            }

            setUriIntent(item.replace(MEBKM_URL, "").replace("\\", "").replace(";", ""));
        }
    }

    void setText(String text) {
        mScannedText = text;
    }

    private Date parseDate(String item) {
        SimpleDateFormat format = new SimpleDateFormat(EVENT_DATE_FORMAT, Locale.getDefault());
        String formattedDate = item.split("T")[0];

        try {
            return format.parse(formattedDate);
        } catch (ParseException e) {
            return new Date();
        }
    }

    private void runIntent(Context context) {
        if (mIntent.resolveActivity(context.getPackageManager()) == null) {
            Toast.makeText(context, context.getString(R.string.quick_reader_no_activity_found),
                    Toast.LENGTH_LONG).show();
        } else {
            context.startActivity(mIntent);
        }
    }

    private void showScannedText(Context context) {
        new AlertDialog.Builder(context)
                .setTitle(R.string.quick_reader_scanned_text_title)
                .setMessage(mScannedText)
                .setNeutralButton(R.string.quick_reader_action_dismiss, null)
                .setPositiveButton(R.string.quick_reader_scanned_text_positive,
                        (dialog, i) -> copyScanned(context))
                .setNegativeButton(R.string.quick_reader_scanned_text_negative,
                        (dialog, i) -> shareScanned(context))
                .show();
    }

    private void copyScanned(Context context) {
        ClipboardManager manager = (ClipboardManager)
                context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (manager != null) {
            manager.setPrimaryClip(ClipData.newPlainText("", mScannedText));
            Toast.makeText(context, context.getString(R.string.quick_reader_copied_message),
                    Toast.LENGTH_LONG).show();
        }
    }

    private void shareScanned(Context context) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, mScannedText);
        context.startActivity(Intent.createChooser(intent,
                context.getString(R.string.quick_reader_share_title)));
    }
}