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 com.android.settings;
18
19import com.android.providers.subscribedfeeds.R;
20
21import android.accounts.Account;
22import android.app.Activity;
23import android.content.ContentResolver;
24import android.os.Bundle;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.AdapterView;
28import android.widget.ArrayAdapter;
29import android.widget.LinearLayout;
30import android.widget.ListAdapter;
31import android.widget.ListView;
32import android.widget.TextView;
33
34/**
35 * Presents multiple options for handling the case where a sync was aborted because there
36 * were too many pending deletes. One option is to force the delete, another is to rollback
37 * the deletes, the third is to do nothing.
38 */
39public class SyncActivityTooManyDeletes extends Activity
40        implements AdapterView.OnItemClickListener {
41
42    private long mNumDeletes;
43    private Account mAccount;
44    private String mAuthority;
45    private String mProvider;
46
47    @Override
48    protected void onCreate(Bundle savedInstanceState) {
49        super.onCreate(savedInstanceState);
50
51        Bundle extras = getIntent().getExtras();
52        if (extras == null) {
53            finish();
54            return;
55        }
56
57        mNumDeletes = extras.getLong("numDeletes");
58        mAccount = (Account) extras.getParcelable("account");
59        mAuthority = extras.getString("authority");
60        mProvider = extras.getString("provider");
61
62        // the order of these must match up with the constants for position used in onItemClick
63        CharSequence[] options = new CharSequence[]{
64                getResources().getText(R.string.sync_really_delete),
65                getResources().getText(R.string.sync_undo_deletes),
66                getResources().getText(R.string.sync_do_nothing)
67        };
68
69        ListAdapter adapter = new ArrayAdapter<CharSequence>(this,
70                android.R.layout.simple_list_item_1,
71                android.R.id.text1,
72                options);
73
74        ListView listView = new ListView(this);
75        listView.setAdapter(adapter);
76        listView.setItemsCanFocus(true);
77        listView.setOnItemClickListener(this);
78
79        TextView textView = new TextView(this);
80        CharSequence tooManyDeletesDescFormat =
81                getResources().getText(R.string.sync_too_many_deletes_desc);
82        textView.setText(String.format(tooManyDeletesDescFormat.toString(),
83                mNumDeletes, mProvider, mAccount.name));
84
85        final LinearLayout ll = new LinearLayout(this);
86        ll.setOrientation(LinearLayout.VERTICAL);
87        final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
88                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0);
89        ll.addView(textView, lp);
90        ll.addView(listView, lp);
91
92        // TODO: consider displaying the icon of the account type
93//        AuthenticatorDescription[] descs = AccountManager.get(this).getAuthenticatorTypes();
94//        for (AuthenticatorDescription desc : descs) {
95//            if (desc.type.equals(mAccount.type)) {
96//                try {
97//                    final Context authContext = createPackageContext(desc.packageName, 0);
98//                    ImageView imageView = new ImageView(this);
99//                    imageView.setImageDrawable(authContext.getResources().getDrawable(desc.iconId));
100//                    ll.addView(imageView, lp);
101//                } catch (PackageManager.NameNotFoundException e) {
102//                }
103//                break;
104//            }
105//        }
106
107        setContentView(ll);
108    }
109
110    public void onItemClick(AdapterView parent, View view, int position, long id) {
111        // the contants for position correspond to the items options array in onCreate()
112        if (position == 0) startSyncReallyDelete();
113        else if (position == 1) startSyncUndoDeletes();
114        finish();
115    }
116
117    private void startSyncReallyDelete() {
118        Bundle extras = new Bundle();
119        extras.putBoolean(ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS, true);
120        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
121        extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
122        extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
123        ContentResolver.requestSync(mAccount, mAuthority, extras);
124    }
125
126    private void startSyncUndoDeletes() {
127        Bundle extras = new Bundle();
128        extras.putBoolean(ContentResolver.SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS, true);
129        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
130        extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
131        extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
132        ContentResolver.requestSync(mAccount, mAuthority, extras);
133    }
134}
135