Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BookmarkBackupController.java « bookmarks « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bd37cf868342ae596cefcd411dd84e5ec2de964a (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
package com.mapswithme.maps.bookmarks;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.maps.auth.Authorizer;
import com.mapswithme.maps.background.Notifier;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.widget.BookmarkBackupView;
import com.mapswithme.util.ConnectionState;
import com.mapswithme.util.DateUtils;
import com.mapswithme.maps.dialog.DialogUtils;
import com.mapswithme.util.NetworkPolicy;
import com.mapswithme.util.SharedPropertiesUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.statistics.Statistics;

import java.util.Date;

import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_AUTH_ERROR;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_BACKUP;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_BACKUP_EXISTS;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_DISK_ERROR;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_INVALID_CALL;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_NETWORK_ERROR;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_NOT_ENOUGH_DISK_SPACE;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_NO_BACKUP;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_RESTORE;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_SUCCESS;
import static com.mapswithme.maps.bookmarks.data.BookmarkManager.CLOUD_USER_INTERRUPTED;
import static com.mapswithme.util.statistics.Statistics.EventName.BM_RESTORE_PROPOSAL_CANCEL;

public class BookmarkBackupController implements Authorizer.Callback,
                                                 BookmarkManager.BookmarksCloudListener
{
  private static final String TAG = BookmarkBackupController.class.getSimpleName();
  private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
  @NonNull
  private final FragmentActivity mContext;
  @NonNull
  private final BookmarkBackupView mBackupView;
  @NonNull
  private final Authorizer mAuthorizer;
  @NonNull
  private final AuthCompleteListener mAuthCompleteListener;
  @NonNull
  private final View.OnClickListener mSignInClickListener = new View.OnClickListener()
  {
    @Override
    public void onClick(View v)
    {
      mAuthorizer.authorize();
      Statistics.INSTANCE.trackBmSyncProposalApproved(false);
    }
  };
  @NonNull
  private final View.OnClickListener mEnableClickListener = new View.OnClickListener()
  {
    @Override
    public void onClick(View v)
    {
      BookmarkManager.INSTANCE.setCloudEnabled(true);
      updateWidget();
      Statistics.INSTANCE.trackBmSyncProposalApproved(mAuthorizer.isAuthorized());
    }
  };
  @NonNull
  private final View.OnClickListener mRestoreClickListener = v ->
  {
    requestRestoring();
    Statistics.INSTANCE.trackBmRestoreProposalClick();
  };
  @Nullable
  private ProgressDialog mRestoringProgressDialog;
  @NonNull
  private final DialogInterface.OnClickListener mRestoreCancelListener = (dialog, which) -> {
    Statistics.INSTANCE.trackEvent(BM_RESTORE_PROPOSAL_CANCEL);
    BookmarkManager.INSTANCE.cancelRestoring();
  };

  BookmarkBackupController(@NonNull FragmentActivity context, @NonNull BookmarkBackupView backupView,
                           @NonNull Authorizer authorizer,
                           @NonNull AuthCompleteListener authCompleteListener)
  {
    mContext = context;
    mBackupView = backupView;
    mAuthorizer = authorizer;
    mAuthCompleteListener = authCompleteListener;
  }

  private void requestRestoring()
  {
    if (!ConnectionState.isConnected())
    {
      DialogInterface.OnClickListener clickListener
          = (dialog, which) -> Utils.showSystemSettings(mContext);
      DialogUtils.showAlertDialog(mContext, R.string.common_check_internet_connection_dialog_title,
                                  R.string.common_check_internet_connection_dialog,
                                  R.string.settings, clickListener, R.string.ok);
      return;
    }

    NetworkPolicy.NetworkPolicyListener policyListener = policy -> {
      LOGGER.d(TAG, "Request bookmark restoring");
      BookmarkManager.INSTANCE.requestRestoring();
    };

    NetworkPolicy.checkNetworkPolicy(mContext.getSupportFragmentManager(), policyListener);
  }

  private void showRestoringProgressDialog()
  {
    if (mRestoringProgressDialog != null && mRestoringProgressDialog.isShowing())
      throw new AssertionError("Previous progress must be dismissed before " +
                               "showing another one!");
    mRestoringProgressDialog = DialogUtils.createModalProgressDialog(mContext,
                                                                     R.string.bookmarks_restore_process,
                                                                     DialogInterface.BUTTON_NEGATIVE,
                                                                     R.string.cancel,
                                                                     mRestoreCancelListener);
    mRestoringProgressDialog.show();
  }

  private void hideRestoringProgressDialog()
  {
    if (mRestoringProgressDialog == null || !mRestoringProgressDialog.isShowing())
      return;

    mRestoringProgressDialog.dismiss();
    mRestoringProgressDialog = null;
  }

  private void updateWidget()
  {
    if (!mAuthorizer.isAuthorized())
    {
      mBackupView.setMessage(mContext.getString(R.string.bookmarks_message_unauthorized_user));
      mBackupView.setBackupButtonLabel(mContext.getString(R.string.authorization_button_sign_in));
      if (mAuthorizer.isAuthorizationInProgress())
      {
        mBackupView.showProgressBar();
        mBackupView.hideBackupButton();
        mBackupView.hideRestoreButton();
      }
      else
      {
        mBackupView.hideProgressBar();
        mBackupView.setBackupClickListener(mSignInClickListener);
        mBackupView.showBackupButton();
        mBackupView.hideRestoreButton();
        Statistics.INSTANCE.trackBmSyncProposalShown(mAuthorizer.isAuthorized());
      }
      return;
    }

    mBackupView.hideProgressBar();

    boolean isEnabled = BookmarkManager.INSTANCE.isCloudEnabled();
    if (isEnabled)
    {
      long backupTime = BookmarkManager.INSTANCE.getLastSynchronizationTimestampInMs();
      String msg;
      if (backupTime > 0)
      {
        msg = mContext.getString(R.string.bookmarks_message_backuped_user,
                                 DateUtils.getShortDateFormatter().format(new Date(backupTime)));
      }
      else
      {
        msg = mContext.getString(R.string.bookmarks_message_unbackuped_user);
      }
      mBackupView.setMessage(msg);
      mBackupView.hideBackupButton();
      mBackupView.setRestoreClickListener(mRestoreClickListener);
      mBackupView.showRestoreButton();
      return;
    }

    // If backup is disabled.
    mBackupView.setMessage(mContext.getString(R.string.bookmarks_message_authorized_user));
    mBackupView.setBackupButtonLabel(mContext.getString(R.string.bookmarks_backup));
    mBackupView.setBackupClickListener(mEnableClickListener);
    mBackupView.showBackupButton();
    mBackupView.hideRestoreButton();
    Statistics.INSTANCE.trackBmSyncProposalShown(mAuthorizer.isAuthorized());
  }

  public void onStart()
  {
    mAuthorizer.attach(this);
    BookmarkManager.INSTANCE.addCloudListener(this);
    mBackupView.setExpanded(SharedPropertiesUtils.getBackupWidgetExpanded());
    updateWidget();
  }

  public void onStop()
  {
    mAuthorizer.detach();
    BookmarkManager.INSTANCE.removeCloudListener(this);
    SharedPropertiesUtils.setBackupWidgetExpanded(mBackupView.getExpanded());
  }

  public void onTargetFragmentResult(int resultCode, @Nullable Intent data)
  {
    mAuthorizer.onTargetFragmentResult(resultCode, data);
  }

  @Override
  public void onAuthorizationStart()
  {
    LOGGER.d(TAG, "onAuthorizationStart");
    updateWidget();
  }

  @Override
  public void onAuthorizationFinish(boolean success)
  {
    LOGGER.d(TAG, "onAuthorizationFinish, success: " + success);
    if (success)
    {
      final Notifier notifier = Notifier.from(mContext.getApplication());
      notifier.cancelNotification(Notifier.ID_IS_NOT_AUTHENTICATED);
      BookmarkManager.INSTANCE.setCloudEnabled(true);
      Statistics.INSTANCE.trackEvent(Statistics.EventName.BM_SYNC_PROPOSAL_ENABLED);
      mAuthCompleteListener.onAuthCompleted();
    }
    else
    {
      Toast.makeText(mContext, R.string.profile_authorization_error, Toast.LENGTH_LONG).show();
      Statistics.INSTANCE.trackBmSyncProposalError(Framework.TOKEN_MAPSME, "Unknown error");
    }
    updateWidget();
  }

  @Override
  public void onSocialAuthenticationError(@Framework.AuthTokenType int type, @Nullable String error)
  {
    LOGGER.w(TAG, "onSocialAuthenticationError, type: " + Statistics.getAuthProvider(type) +
                  " error: " + error);
    Statistics.INSTANCE.trackBmSyncProposalError(type, error);
  }

  @Override
  public void onSocialAuthenticationCancel(@Framework.AuthTokenType int type)
  {
    LOGGER.i(TAG, "onSocialAuthenticationCancel, type: " + Statistics.getAuthProvider(type));
    Statistics.INSTANCE.trackBmSyncProposalError(type, "Cancel");
  }

  @Override
  public void onSynchronizationStarted(@BookmarkManager.SynchronizationType int type)
  {
    LOGGER.d(TAG, "onSynchronizationStarted, type: " + Statistics.getSynchronizationType(type));
    switch (type)
    {
      case CLOUD_BACKUP:
        Statistics.INSTANCE.trackEvent(Statistics.EventName.BM_SYNC_STARTED);
        break;
      case CLOUD_RESTORE:
        showRestoringProgressDialog();
        break;
      default:
        throw new AssertionError("Unsupported synchronization type: " + type);
    }
    updateWidget();
  }

  @Override
  public void onSynchronizationFinished(@BookmarkManager.SynchronizationType int type,
                                        @BookmarkManager.SynchronizationResult int result,
                                        @NonNull String errorString)
  {
    LOGGER.d(TAG, "onSynchronizationFinished, type: " + Statistics.getSynchronizationType(type)
                  + ", result: " + result + ", errorString = " + errorString);
    Statistics.INSTANCE.trackBmSynchronizationFinish(type, result, errorString);
    hideRestoringProgressDialog();

    updateWidget();

    if (type == CLOUD_BACKUP)
      return;

    // Restoring is finished.
    switch (result)
    {
      case CLOUD_AUTH_ERROR:
      case CLOUD_NETWORK_ERROR:
        DialogInterface.OnClickListener clickListener
            = (dialog, which) -> requestRestoring();
        DialogUtils.showAlertDialog(mContext, R.string.error_server_title,
                                    R.string.error_server_message, R.string.try_again,
                                    clickListener, R.string.cancel);
        break;
      case CLOUD_DISK_ERROR:
        DialogUtils.showAlertDialog(mContext, R.string.dialog_routing_system_error,
                                    R.string.error_system_message);
        break;
      case CLOUD_INVALID_CALL:
        throw new AssertionError("Check correctness of cloud api usage!");
      case CLOUD_USER_INTERRUPTED:
      case CLOUD_SUCCESS:
        // Do nothing.
        break;
      default:
        throw new AssertionError("Unsupported synchronization result: " + result + "," +
                                 " error message: " + errorString);
    }
  }

  @Override
  public void onRestoreRequested(@BookmarkManager.RestoringRequestResult int result,
                                 @NonNull String deviceName, long backupTimestampInMs)
  {
    LOGGER.d(TAG, "onRestoreRequested, result: " + result + ", deviceName = " + deviceName +
                  ", backupTimestampInMs = " + backupTimestampInMs);
    Statistics.INSTANCE.trackBmRestoringRequestResult(result);
    hideRestoringProgressDialog();

    switch (result)
    {
      case CLOUD_BACKUP_EXISTS:
        String backupDate = DateUtils.getShortDateFormatter().format(new Date(backupTimestampInMs));
        DialogInterface.OnClickListener clickListener = (dialog, which) -> {
          showRestoringProgressDialog();
          BookmarkManager.INSTANCE.applyRestoring();
        };
        String msg = mContext.getString(R.string.bookmarks_restore_message, backupDate, deviceName);
        DialogUtils.showAlertDialog(mContext, R.string.bookmarks_restore_title, msg,
                                    R.string.restore, clickListener, R.string.cancel, mRestoreCancelListener);
        break;
      case CLOUD_NO_BACKUP:
        DialogUtils.showAlertDialog(mContext, R.string.bookmarks_restore_empty_title,
                                    R.string.bookmarks_restore_empty_message,
                                    R.string.ok, mRestoreCancelListener);
        break;
      case CLOUD_NOT_ENOUGH_DISK_SPACE:
        DialogInterface.OnClickListener tryAgainListener
            = (dialog, which) -> BookmarkManager.INSTANCE.requestRestoring();
        DialogUtils.showAlertDialog(mContext, R.string.routing_not_enough_space,
                                    R.string.not_enough_free_space_on_sdcard,
                                    R.string.try_again, tryAgainListener, R.string.cancel,
                                    mRestoreCancelListener);
        break;
      default:
        throw new AssertionError("Unsupported restoring request result: " + result);
    }
  }

  @Override
  public void onRestoredFilesPrepared()
  {
    LOGGER.d(TAG, "onRestoredFilesPrepared()");
    if (mRestoringProgressDialog == null || !mRestoringProgressDialog.isShowing())
      return;

    Button cancelButton = mRestoringProgressDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
    if (cancelButton == null)
      throw new AssertionError("Restoring progress dialog must contain cancel button!");

    cancelButton.setEnabled(false);
  }
}