1/*
2 * Copyright (C) 2012 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.ui;
19
20import android.app.Fragment;
21import android.app.LoaderManager;
22import android.content.CursorLoader;
23import android.content.Intent;
24import android.content.Loader;
25import android.database.Cursor;
26import android.os.Bundle;
27import android.provider.Settings;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
31
32import com.android.mail.R;
33import com.android.mail.providers.Account;
34import com.android.mail.providers.UIProvider.SyncStatus;
35
36public class WaitFragment extends Fragment implements View.OnClickListener,
37        LoaderManager.LoaderCallbacks<Cursor> {
38    // Keys used to pass data to {@link WaitFragment}.
39    private static final String ACCOUNT_KEY = "account";
40
41    private static final String EXPECTING_MESSAGES_KEY = "expectingMessages";
42
43    private static final int MANUAL_SYNC_LOADER = 0;
44
45
46    private Account mAccount;
47
48    private LayoutInflater mInflater;
49
50    private boolean mExpectingMessages;
51
52    // Public no-args constructor needed for fragment re-instantiation
53    public WaitFragment() {}
54
55    public static WaitFragment newInstance(Account account, boolean expectingMessages) {
56        WaitFragment fragment = new WaitFragment();
57
58        final Bundle args = new Bundle();
59        args.putParcelable(ACCOUNT_KEY, account);
60        args.putBoolean(EXPECTING_MESSAGES_KEY, expectingMessages);
61        fragment.setArguments(args);
62        return fragment;
63    }
64
65    @Override
66    public void onCreate(Bundle savedInstanceState) {
67        super.onCreate(savedInstanceState);
68
69        Bundle args = getArguments();
70        mAccount = (Account)args.getParcelable(ACCOUNT_KEY);
71        mExpectingMessages = args.getBoolean(EXPECTING_MESSAGES_KEY, false);
72    }
73
74    @Override
75    public View onCreateView(
76            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
77        mInflater = inflater;
78        ViewGroup wrapper = (ViewGroup) mInflater
79                .inflate(R.layout.wait_container, container, false);
80        wrapper.addView(getContent(wrapper));
81        return wrapper;
82    }
83
84    private View getContent(ViewGroup root) {
85        final View view;
86        if (mAccount != null
87                && (mAccount.syncStatus & SyncStatus.MANUAL_SYNC_REQUIRED)
88                    == SyncStatus.MANUAL_SYNC_REQUIRED) {
89            // A manual sync is required
90            view = mInflater.inflate(R.layout.wait_for_manual_sync, root, false);
91
92            view.findViewById(R.id.manual_sync).setOnClickListener(this);
93            view.findViewById(R.id.change_sync_settings).setOnClickListener(this);
94
95        } else if (mExpectingMessages) {
96            view = mInflater.inflate(R.layout.loading_messages, root, false);
97        } else {
98            view = mInflater.inflate(R.layout.wait_default, root, false);
99        }
100
101        return view;
102    }
103
104    public void updateAccount(Account account) {
105        mAccount = account;
106        ViewGroup parent = (ViewGroup) getView();
107        if (parent != null) {
108            parent.removeAllViews();
109            parent.addView(getContent(parent));
110        }
111    }
112
113    Account getAccount() {
114        return mAccount;
115    }
116
117    @Override
118    public void onClick(View v) {
119        final int id = v.getId();
120
121        if (id == R.id.change_sync_settings) {
122            Intent intent = new Intent(Settings.ACTION_SYNC_SETTINGS);
123            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
124            startActivity(intent);
125        } else if (id == R.id.manual_sync) {
126            if (mAccount != null && mAccount.manualSyncUri != null) {
127                getLoaderManager().initLoader(MANUAL_SYNC_LOADER, null, this);
128            }
129        }
130    }
131
132    @Override
133    public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
134        // Tell the account to sync manually. As a side effect, updates will come into the
135        // controller for this fragment and update it as necessary.
136        return new CursorLoader(getActivity(), mAccount.manualSyncUri, null, null, null, null);
137    }
138
139    @Override
140    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
141        // Do nothing.
142    }
143
144    @Override
145    public void onLoaderReset(Loader<Cursor> arg0) {
146        // Do nothing.
147    }
148}