summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/activity/gpoddernet/GpodnetAuthenticationActivity.java
blob: 8ede947c599a94d404dfb4772f1bf9114ad272e9 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package de.danoeh.antennapod.activity.gpoddernet;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewFlipper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.gpoddernet.GpodnetService;
import de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException;
import de.danoeh.antennapod.core.gpoddernet.model.GpodnetDevice;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.GpodnetSyncService;

/**
 * Guides the user through the authentication process
 * Step 1: Request username and password from user
 * Step 2: Choose device from a list of available devices or create a new one
 * Step 3: Choose from a list of actions
 */
public class GpodnetAuthenticationActivity extends AppCompatActivity {
    private static final String TAG = "GpodnetAuthActivity";

    private static final String CURRENT_STEP = "current_step";

    private ViewFlipper viewFlipper;

    private static final int STEP_DEFAULT = -1;
    private static final int STEP_LOGIN = 0;
    private static final int STEP_DEVICE = 1;
    private static final int STEP_FINISH = 2;

    private int currentStep = -1;

    private GpodnetService service;
    private volatile String username;
    private volatile String password;
    private volatile GpodnetDevice selectedDevice;

    View[] views;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(UserPreferences.getTheme());
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        setContentView(R.layout.gpodnetauth_activity);
        service = new GpodnetService();

        viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        views = new View[]{
                inflater.inflate(R.layout.gpodnetauth_credentials, viewFlipper, false),
                inflater.inflate(R.layout.gpodnetauth_device, viewFlipper, false),
                inflater.inflate(R.layout.gpodnetauth_finish, viewFlipper, false)
        };
        for (View view : views) {
            viewFlipper.addView(view);
        }
        advance();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (service != null) {
            service.shutdown();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    }

    private void setupLoginView(View view) {
        final EditText username = (EditText) view.findViewById(R.id.etxtUsername);
        final EditText password = (EditText) view.findViewById(R.id.etxtPassword);
        final Button login = (Button) view.findViewById(R.id.butLogin);
        final TextView txtvError = (TextView) view.findViewById(R.id.txtvError);
        final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progBarLogin);

        password.setOnEditorActionListener((v, actionID, event) ->
            actionID == EditorInfo.IME_ACTION_GO && login.performClick());

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String usernameStr = username.getText().toString();
                final String passwordStr = password.getText().toString();

                if (BuildConfig.DEBUG) Log.d(TAG, "Checking login credentials");
                AsyncTask<GpodnetService, Void, Void> authTask = new AsyncTask<GpodnetService, Void, Void>() {

                    volatile Exception exception;

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        login.setEnabled(false);
                        progressBar.setVisibility(View.VISIBLE);
                        txtvError.setVisibility(View.GONE);
                        // hide the keyboard
                        InputMethodManager inputManager = (InputMethodManager)
                                getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputManager.hideSoftInputFromWindow(login.getWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);

                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                        login.setEnabled(true);
                        progressBar.setVisibility(View.GONE);

                        if (exception == null) {
                            advance();
                        } else {
                            txtvError.setText(exception.getCause().getMessage());
                            txtvError.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    protected Void doInBackground(GpodnetService... params) {
                        try {
                            params[0].authenticate(usernameStr, passwordStr);
                            GpodnetAuthenticationActivity.this.username = usernameStr;
                            GpodnetAuthenticationActivity.this.password = passwordStr;
                        } catch (GpodnetServiceException e) {
                            e.printStackTrace();
                            exception = e;
                        }
                        return null;
                    }
                };
                if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
                    authTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, service);
                } else {
                    authTask.execute();
                }
            }
        });
    }

    private void setupDeviceView(View view) {
        final EditText deviceID = (EditText) view.findViewById(R.id.etxtDeviceID);
        final EditText caption = (EditText) view.findViewById(R.id.etxtCaption);
        final Button createNewDevice = (Button) view.findViewById(R.id.butCreateNewDevice);
        final Button chooseDevice = (Button) view.findViewById(R.id.butChooseExistingDevice);
        final TextView txtvError = (TextView) view.findViewById(R.id.txtvError);
        final ProgressBar progBarCreateDevice = (ProgressBar) view.findViewById(R.id.progbarCreateDevice);
        final Spinner spinnerDevices = (Spinner) view.findViewById(R.id.spinnerChooseDevice);


        // load device list
        final AtomicReference<List<GpodnetDevice>> devices = new AtomicReference<>();
        new AsyncTask<GpodnetService, Void, List<GpodnetDevice>>() {

            private volatile Exception exception;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                chooseDevice.setEnabled(false);
                spinnerDevices.setEnabled(false);
                createNewDevice.setEnabled(false);
            }

            @Override
            protected void onPostExecute(List<GpodnetDevice> gpodnetDevices) {
                super.onPostExecute(gpodnetDevices);
                if (gpodnetDevices != null) {
                    List<String> deviceNames = new ArrayList<>();
                    for (GpodnetDevice device : gpodnetDevices) {
                        deviceNames.add(device.getCaption());
                    }
                    spinnerDevices.setAdapter(new ArrayAdapter<>(GpodnetAuthenticationActivity.this,
                            android.R.layout.simple_spinner_dropdown_item, deviceNames));
                    spinnerDevices.setEnabled(true);
                    if (!deviceNames.isEmpty()) {
                        chooseDevice.setEnabled(true);
                    }
                    devices.set(gpodnetDevices);
                    deviceID.setText(generateDeviceID(gpodnetDevices));
                    createNewDevice.setEnabled(true);
                }
            }

            @Override
            protected List<GpodnetDevice> doInBackground(GpodnetService... params) {
                try {
                    return params[0].getDevices(username);
                } catch (GpodnetServiceException e) {
                    e.printStackTrace();
                    exception = e;
                    return null;
                }
            }
        }.execute(service);


        createNewDevice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkDeviceIDText(deviceID, caption, txtvError, devices.get())) {
                    final String deviceStr = deviceID.getText().toString();
                    final String captionStr = caption.getText().toString();

                    new AsyncTask<GpodnetService, Void, GpodnetDevice>() {

                        private volatile Exception exception;

                        @Override
                        protected void onPreExecute() {
                            super.onPreExecute();
                            createNewDevice.setEnabled(false);
                            chooseDevice.setEnabled(false);
                            progBarCreateDevice.setVisibility(View.VISIBLE);
                            txtvError.setVisibility(View.GONE);
                        }

                        @Override
                        protected void onPostExecute(GpodnetDevice result) {
                            super.onPostExecute(result);
                            createNewDevice.setEnabled(true);
                            chooseDevice.setEnabled(true);
                            progBarCreateDevice.setVisibility(View.GONE);
                            if (exception == null) {
                                selectedDevice = result;
                                advance();
                            } else {
                                txtvError.setText(exception.getMessage());
                                txtvError.setVisibility(View.VISIBLE);
                            }
                        }

                        @Override
                        protected GpodnetDevice doInBackground(GpodnetService... params) {
                            try {
                                params[0].configureDevice(username, deviceStr, captionStr, GpodnetDevice.DeviceType.MOBILE);
                                return new GpodnetDevice(deviceStr, captionStr, GpodnetDevice.DeviceType.MOBILE.toString(), 0);
                            } catch (GpodnetServiceException e) {
                                e.printStackTrace();
                                exception = e;
                            }
                            return null;
                        }
                    }.execute(service);
                }
            }
        });

        chooseDevice.setOnClickListener(v -> {
            final int position = spinnerDevices.getSelectedItemPosition();
            if (position != AdapterView.INVALID_POSITION) {
                selectedDevice = devices.get().get(position);
                advance();
            }
        });
    }


    private String generateDeviceID(List<GpodnetDevice> gpodnetDevices) {
        // devices names must be of a certain form:
        // https://gpoddernet.readthedocs.org/en/latest/api/reference/general.html#devices
        // This is more restrictive than needed, but I think it makes for more readable names.
        String baseId = Build.MODEL.replaceAll("\\W", "");
        String id = baseId;
        int num = 0;

        while (isDeviceWithIdInList(id, gpodnetDevices)) {
            id = baseId + "_" + num;
            num++;
        }

        return id;
    }

    private boolean isDeviceWithIdInList(String id, List<GpodnetDevice> gpodnetDevices) {
        if (gpodnetDevices == null) {
            return false;
        }
        for (GpodnetDevice device : gpodnetDevices) {
            if (device.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    private boolean checkDeviceIDText(EditText deviceID, EditText caption, TextView txtvError, List<GpodnetDevice> devices) {
        String text = deviceID.getText().toString();
        if (text.length() == 0) {
            txtvError.setText(R.string.gpodnetauth_device_errorEmpty);
            txtvError.setVisibility(View.VISIBLE);
            return false;
        } else if (caption.length() == 0) {
            txtvError.setText(R.string.gpodnetauth_device_caption_errorEmpty);
            txtvError.setVisibility(View.VISIBLE);
            return false;
        } else {
            if (devices != null) {
                if (isDeviceWithIdInList(text, devices)) {
                    txtvError.setText(R.string.gpodnetauth_device_errorAlreadyUsed);
                    txtvError.setVisibility(View.VISIBLE);
                    return false;
                }
                txtvError.setVisibility(View.GONE);
                return true;
            }
            return true;
        }

    }

    private void setupFinishView(View view) {
        final Button sync = (Button) view.findViewById(R.id.butSyncNow);
        final Button back = (Button) view.findViewById(R.id.butGoMainscreen);

        sync.setOnClickListener(v -> {
            GpodnetSyncService.sendSyncIntent(GpodnetAuthenticationActivity.this);
            finish();
        });
        back.setOnClickListener(v -> {
            Intent intent = new Intent(GpodnetAuthenticationActivity.this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        });
    }

    private void writeLoginCredentials() {
        if (BuildConfig.DEBUG) Log.d(TAG, "Writing login credentials");
        GpodnetPreferences.setUsername(username);
        GpodnetPreferences.setPassword(password);
        GpodnetPreferences.setDeviceID(selectedDevice.getId());
    }

    private void advance() {
        if (currentStep < STEP_FINISH) {

            View view = views[currentStep + 1];
            if (currentStep == STEP_DEFAULT) {
                setupLoginView(view);
            } else if (currentStep == STEP_LOGIN) {
                if (username == null || password == null) {
                    throw new IllegalStateException("Username and password must not be null here");
                } else {
                    setupDeviceView(view);
                }
            } else if (currentStep == STEP_DEVICE) {
                if (selectedDevice == null) {
                    throw new IllegalStateException("Device must not be null here");
                } else {
                    writeLoginCredentials();
                    setupFinishView(view);
                }
            }
            if (currentStep != STEP_DEFAULT) {
                viewFlipper.showNext();
            }
            currentStep++;
        } else {
            finish();
        }
    }
}