summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/ui/ConversationSyncDisabledTipView.java
blob: 30a80be015f629863d27a895fb2799f7b364e5e2 (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
/*
 * Copyright (C) 2013 The Android Open Source 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 com.android.mail.ui;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.Animator.AnimatorListener;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.android.mail.R;
import com.android.mail.analytics.Analytics;
import com.android.mail.browse.ConversationCursor;
import com.android.mail.preferences.AccountPreferences;
import com.android.mail.preferences.MailPrefs;
import com.android.mail.providers.Account;
import com.android.mail.providers.Folder;
import com.android.mail.utils.LogTag;
import com.android.mail.utils.LogUtils;
import com.android.mail.utils.Utils;

/**
 * A tip displayed on top of conversation view to indicate that Gmail sync is
 * currently disabled on this account.
 */
public class ConversationSyncDisabledTipView extends FrameLayout
        implements ConversationSpecialItemView, SwipeableItemView {

    private static final String LOG_TAG = LogTag.getLogTag();

    private static int sScrollSlop = 0;
    private static int sShrinkAnimationDuration;

    private Account mAccount = null;
    private Folder mFolder = null;
    private final MailPrefs mMailPrefs;
    private AccountPreferences mAccountPreferences;
    private AnimatedAdapter mAdapter;
    private Activity mActivity;

    private View mSwipeableContent;
    private TextView mText1;
    private TextView mText2;
    private View mTextArea;
    private SpannableString mEnableSyncInAccountSettingsText;
    private final OnClickListener mAutoSyncOffTextClickedListener;
    private final OnClickListener mAccountSyncOffTextClickedListener;

    private int mAnimatedHeight = -1;

    private int mReasonSyncOff = ReasonSyncOff.NONE;

    public interface ReasonSyncOff {
        // Background sync is enabled for current account, do not display this tip
        public static final int NONE = 0;
        // Global auto-sync (affects all apps and all accounts) is turned off
        public static final int AUTO_SYNC_OFF = 1;
        // Global auto-sync is on, but Gmail app level sync is disabled for this particular account
        public static final int ACCOUNT_SYNC_OFF = 2;
    }

    public ConversationSyncDisabledTipView(final Context context) {
        this(context, null);
    }

    public ConversationSyncDisabledTipView(final Context context, final AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ConversationSyncDisabledTipView(
            final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);

        final Resources resources = context.getResources();

        if (sScrollSlop == 0) {
            sScrollSlop = resources.getInteger(R.integer.swipeScrollSlop);
            sShrinkAnimationDuration = resources.getInteger(
                    R.integer.shrink_animation_duration);
        }

        mMailPrefs = MailPrefs.get(context);

        mAutoSyncOffTextClickedListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                final TurnAutoSyncOnDialog dialog = TurnAutoSyncOnDialog.newInstance(
                        mAccount.getAccountManagerAccount(), mAccount.syncAuthority);
                dialog.show(mActivity.getFragmentManager(), TurnAutoSyncOnDialog.DIALOG_TAG);
            }
        };

        mAccountSyncOffTextClickedListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.showAccountSettings(getContext(), mAccount);
            }
        };

        // Create the "Turn on in Account settings." text where "Account settings" appear as
        // a blue link.
        final String subString = resources.getString(R.string.account_settings_param);
        final String entireString = resources.getString(
                R.string.enable_sync_in_account_settings, subString);
        mEnableSyncInAccountSettingsText = new SpannableString(entireString);
        final int index = entireString.indexOf(subString);
        mEnableSyncInAccountSettingsText.setSpan(
                new TextAppearanceSpan(context, R.style.LinksInTipTextAppearance),
                index,
                index + subString.length(),
                0);
    }

    public void bindAccount(Account account, ControllableActivity activity) {
        mAccount = account;
        mAccountPreferences = AccountPreferences.get(getContext(), account);
        mActivity = (Activity) activity;
    }

    @Override
    public void onGetView() {
        // Do nothing
    }

    @Override
    protected void onFinishInflate() {
        mSwipeableContent = findViewById(R.id.swipeable_content);

        mText1 = (TextView) findViewById(R.id.text_line1);
        mText2 = (TextView) findViewById(R.id.text_line2);
        mTextArea = findViewById(R.id.text_area);

        findViewById(R.id.dismiss_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    @Override
    public void onUpdate(Folder folder, ConversationCursor cursor) {
        mFolder = folder;
    }

    @Override
    public boolean getShouldDisplayInList() {
        if (mAccount == null || mAccount.syncAuthority == null) {
            return false;
        }

        // Do not show this message for folders/labels that are not set to sync.
        if (mFolder == null || mFolder.syncWindow <= 0) {
            return false;
        }

        setReasonSyncOff(calculateReasonSyncOff(mMailPrefs, mAccount, mAccountPreferences));

        if (mReasonSyncOff != ReasonSyncOff.NONE) {
            LogUtils.i(LOG_TAG, "Sync is off with reason %d", mReasonSyncOff);
        }

        switch (mReasonSyncOff) {
            case ReasonSyncOff.AUTO_SYNC_OFF:
                return (mMailPrefs.getNumOfDismissesForAutoSyncOff() == 0);
            case ReasonSyncOff.ACCOUNT_SYNC_OFF:
                return (mAccountPreferences.getNumOfDismissesForAccountSyncOff() == 0);
            default:
                return false;
        }
    }

    public static int calculateReasonSyncOff(MailPrefs mailPrefs,
            Account account, AccountPreferences accountPreferences) {
        if (!ContentResolver.getMasterSyncAutomatically()) {
            // Global sync is turned off
            accountPreferences.resetNumOfDismissesForAccountSyncOff();
            // Logging to track down bug where this tip is being showing when it shouldn't be.
            LogUtils.i(LOG_TAG, "getMasterSyncAutomatically() return false");
            return ReasonSyncOff.AUTO_SYNC_OFF;
        } else {
            // Global sync is on, clear the number of times users has dismissed this
            // warning so that next time global sync is off, warning gets displayed again.
            mailPrefs.resetNumOfDismissesForAutoSyncOff();

            // Now check for whether account level sync is on/off.
            android.accounts.Account acct = account.getAccountManagerAccount();
            if (!TextUtils.isEmpty(account.syncAuthority) &&
                    !ContentResolver.getSyncAutomatically(acct, account.syncAuthority)) {
                // Account level sync is off
                return ReasonSyncOff.ACCOUNT_SYNC_OFF;
            } else {
                // Account sync is on, clear the number of times users has dismissed this
                // warning so that next time sync is off, warning gets displayed again.
                accountPreferences.resetNumOfDismissesForAccountSyncOff();
                return ReasonSyncOff.NONE;
            }
        }
    }

    private void setReasonSyncOff(int reason) {
        if (mReasonSyncOff != reason) {
            mReasonSyncOff = reason;
            switch (mReasonSyncOff) {
                case ReasonSyncOff.AUTO_SYNC_OFF:
                    mText1.setText(R.string.auto_sync_off);
                    mText2.setText(R.string.tap_to_enable_sync);
                    mText2.setVisibility(View.VISIBLE);
                    mTextArea.setClickable(true);
                    mTextArea.setOnClickListener(mAutoSyncOffTextClickedListener);
                    break;
                case ReasonSyncOff.ACCOUNT_SYNC_OFF:
                    mText1.setText(R.string.account_sync_off);
                    mText2.setText(mEnableSyncInAccountSettingsText);
                    mText2.setVisibility(View.VISIBLE);
                    mTextArea.setClickable(true);
                    mTextArea.setOnClickListener(mAccountSyncOffTextClickedListener);
                    break;
                default:
                    // Doesn't matter what mText is since this view is not displayed
            }
        }
    }

    @Override
    public int getPosition() {
        // We want this teaser to go before the first real conversation
        return 0;
    }

    @Override
    public void setAdapter(AnimatedAdapter adapter) {
        mAdapter = adapter;
    }

    @Override
    public void bindFragment(LoaderManager loaderManager, final Bundle savedInstanceState) {
    }

    @Override
    public void cleanup() {
    }

    @Override
    public void onConversationSelected() {
        // DO NOTHING
    }

    @Override
    public void onCabModeEntered() {
    }

    @Override
    public void onCabModeExited() {
        // Do nothing
    }

    @Override
    public void onConversationListVisibilityChanged(final boolean visible) {
        // Do nothing
    }

    @Override
    public void saveInstanceState(final Bundle outState) {
        // Do nothing
    }

    @Override
    public boolean acceptsUserTaps() {
        return true;
    }

    @Override
    public void dismiss() {
        final String reason;
        switch (mReasonSyncOff) {
            case ReasonSyncOff.AUTO_SYNC_OFF:
                mMailPrefs.incNumOfDismissesForAutoSyncOff();
                reason = "auto_sync_off";
                break;
            case ReasonSyncOff.ACCOUNT_SYNC_OFF:
                mAccountPreferences.incNumOfDismissesForAccountSyncOff();
                reason = "account_sync_off";
                break;
            default:
                reason = null;
                break;
        }
        Analytics.getInstance().sendEvent("list_swipe", "sync_disabled_tip", reason, 0);
        startDestroyAnimation();
    }

    @Override
    public SwipeableView getSwipeableView() {
        return SwipeableView.from(mSwipeableContent);
    }

    @Override
    public boolean canChildBeDismissed() {
        return true;
    }

    @Override
    public float getMinAllowScrollDistance() {
        return sScrollSlop;
    }

    private void startDestroyAnimation() {
        final int start = getHeight();
        final int end = 0;
        mAnimatedHeight = start;
        final ObjectAnimator heightAnimator =
                ObjectAnimator.ofInt(this, "animatedHeight", start, end);
        heightAnimator.setInterpolator(new DecelerateInterpolator(2.0f));
        heightAnimator.setDuration(sShrinkAnimationDuration);
        heightAnimator.addListener(new AnimatorListener() {
            @Override
            public void onAnimationStart(final Animator animation) {
                // Do nothing
            }

            @Override
            public void onAnimationRepeat(final Animator animation) {
                // Do nothing
            }

            @Override
            public void onAnimationEnd(final Animator animation) {
                // We should no longer exist, so notify the adapter
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onAnimationCancel(final Animator animation) {
                // Do nothing
            }
        });
        heightAnimator.start();
    }

    /**
     * This method is used by the animator.  It is explicitly kept in proguard.flags to prevent it
     * from being removed, inlined, or obfuscated.
     * Edit ./vendor/unbundled/packages/apps/UnifiedGmail/proguard.flags
     * In the future, we want to use @Keep
     */
    public void setAnimatedHeight(final int height) {
        mAnimatedHeight = height;
        requestLayout();
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        if (mAnimatedHeight == -1) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        } else {
            setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), mAnimatedHeight);
        }
    }

    @Override
    public boolean commitLeaveBehindItem() {
        // This view has no leave-behind
        return false;
    }
}