1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content;
18
19import com.android.internal.R;
20import android.accounts.Account;
21import android.app.Activity;
22import android.os.Bundle;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.AdapterView;
26import android.widget.ArrayAdapter;
27import android.widget.LinearLayout;
28import android.widget.ListAdapter;
29import android.widget.ListView;
30import android.widget.TextView;
31
32/**
33 * Presents multiple options for handling the case where a sync was aborted because there
34 * were too many pending deletes. One option is to force the delete, another is to rollback
35 * the deletes, the third is to do nothing.
36 * @hide
37 */
38public class SyncActivityTooManyDeletes extends Activity
39        implements AdapterView.OnItemClickListener {
40
41    private long mNumDeletes;
42    private Account mAccount;
43    private String mAuthority;
44    private String mProvider;
45
46    @Override
47    protected void onCreate(Bundle savedInstanceState) {
48        super.onCreate(savedInstanceState);
49
50        Bundle extras = getIntent().getExtras();
51        if (extras == null) {
52            finish();
53            return;
54        }
55
56        mNumDeletes = extras.getLong("numDeletes");
57        mAccount = (Account) extras.getParcelable("account");
58        mAuthority = extras.getString("authority");
59        mProvider = extras.getString("provider");
60
61        // the order of these must match up with the constants for position used in onItemClick
62        CharSequence[] options = new CharSequence[]{
63                getResources().getText(R.string.sync_really_delete),
64                getResources().getText(R.string.sync_undo_deletes),
65                getResources().getText(R.string.sync_do_nothing)
66        };
67
68        ListAdapter adapter = new ArrayAdapter<CharSequence>(this,
69                android.R.layout.simple_list_item_1,
70                android.R.id.text1,
71                options);
72
73        ListView listView = new ListView(this);
74        listView.setAdapter(adapter);
75        listView.setItemsCanFocus(true);
76        listView.setOnItemClickListener(this);
77
78        TextView textView = new TextView(this);
79        CharSequence tooManyDeletesDescFormat =
80                getResources().getText(R.string.sync_too_many_deletes_desc);
81        textView.setText(String.format(tooManyDeletesDescFormat.toString(),
82                mNumDeletes, mProvider, mAccount.name));
83
84        final LinearLayout ll = new LinearLayout(this);
85        ll.setOrientation(LinearLayout.VERTICAL);
86        final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
87                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0);
88        ll.addView(textView, lp);
89        ll.addView(listView, lp);
90
91        // TODO: consider displaying the icon of the account type
92//        AuthenticatorDescription[] descs = AccountManager.get(this).getAuthenticatorTypes();
93//        for (AuthenticatorDescription desc : descs) {
94//            if (desc.type.equals(mAccount.type)) {
95//                try {
96//                    final Context authContext = createPackageContext(desc.packageName, 0);
97//                    ImageView imageView = new ImageView(this);
98//                    imageView.setImageDrawable(authContext.getDrawable(desc.iconId));
99//                    ll.addView(imageView, lp);
100//                } catch (PackageManager.NameNotFoundException e) {
101//                }
102//                break;
103//            }
104//        }
105
106        setContentView(ll);
107    }
108
109    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
110        // the constants for position correspond to the items options array in onCreate()
111        if (position == 0) startSyncReallyDelete();
112        else if (position == 1) startSyncUndoDeletes();
113        finish();
114    }
115
116    private void startSyncReallyDelete() {
117        Bundle extras = new Bundle();
118        extras.putBoolean(ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS, true);
119        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
120        extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
121        extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
122        ContentResolver.requestSync(mAccount, mAuthority, extras);
123    }
124
125    private void startSyncUndoDeletes() {
126        Bundle extras = new Bundle();
127        extras.putBoolean(ContentResolver.SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS, true);
128        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
129        extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
130        extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
131        ContentResolver.requestSync(mAccount, mAuthority, extras);
132    }
133}
134