summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/preferences/WebsiteSettingsFragment.java
blob: 96ed983d0a8d6f2c260bf63d911a1a824e4e852e (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
/*
 * Copyright (C) 2009 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.browser.preferences;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.browser.R;
import com.android.browser.WebStorageSizeManager;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.codeaurora.swe.GeolocationPermissions;
import org.codeaurora.swe.PermissionsServiceFactory;
import org.codeaurora.swe.WebRefiner;
import org.codeaurora.swe.WebStorage;

/**
 * Manage the settings for an origin.
 * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
 * and Geolocation.
 */
public class WebsiteSettingsFragment extends ListFragment implements OnClickListener {
    private SiteAdapter mAdapter = null;

    private class Site {
        private String mOrigin;
        private String mTitle;
        private Bitmap mIcon;

        public Site(String origin) {
            mOrigin = origin;
            mTitle = null;
            mIcon = null;
            PermissionsServiceFactory.getFavicon(origin, getActivity(),
                    new ValueCallback<Bitmap>() {
                        @Override
                        public void onReceiveValue(Bitmap value) {
                            mIcon = value;
                        }
                    });
        }

        public String getOrigin() {
            return mOrigin;
        }

        public void setTitle(String title) {
            mTitle = title;
        }

        public void setIcon(Bitmap icon) {
            mIcon = icon;
        }

        public Bitmap getIcon() {
            return mIcon;
        }

        public String getPrettyOrigin() {
            return mTitle == null ? null : hideHttp(mOrigin);
        }

        public String getPrettyTitle() {
            return mTitle == null ? hideHttp(mOrigin) : mTitle;
        }

        private String hideHttp(String str) {
            if (str == null)
                return null;
            Uri uri = Uri.parse(str);
            return "http".equals(uri.getScheme()) ?  str.substring(7) : str;
        }
    }

    class SiteAdapter extends ArrayAdapter<Site>
            implements AdapterView.OnItemClickListener {
        private int mResource;
        private LayoutInflater mInflater;
        private Bitmap mDefaultIcon;
        private PermissionsServiceFactory.PermissionsService mPermServ;
        private boolean mReady;

        public SiteAdapter(Context context, int rsc) {
            super(context, rsc);
            mResource = rsc;
            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mDefaultIcon = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_deco_favicon_normal);

            mReady = false;
            askForOrigins();
        }

        public void askForOrigins() {
            if (mPermServ == null) {
                PermissionsServiceFactory.getPermissionsService(
                        new ValueCallback<PermissionsServiceFactory.PermissionsService>() {
                            @Override
                            public void onReceiveValue(
                                    PermissionsServiceFactory.PermissionsService value) {
                                mPermServ = value;
                                Map<String, Site> sites = new HashMap<>();

                                Set<String> origins = mPermServ.getOrigins();
                                for (String origin : origins) {
                                    if (!TextUtils.isEmpty(origin))
                                        sites.put(origin, new Site(origin));
                                }

                                // Create a map from host to origin. This is used to add metadata
                                // (title, icon) for this origin from the bookmarks DB. We must do
                                // the DB access on a background thread.
                                //new UpdateFromBookmarksDbTask(ctx, sites).execute();

                                populateOrigins(sites);
                                mReady = true;
                            }
                        }
                );
            }
        }

        public void deleteAllOrigins() {
            if (mPermServ != null) {
                Set<String> origins = mPermServ.getOrigins();
                String[] originArray = origins.toArray(new String[origins.size()]);

                // purge the permissionservice since its not needed
                mPermServ.purge();
                mPermServ = null;

                // reset all site settings
                PermissionsServiceFactory.resetSiteSettings();

                WebRefiner refiner = WebRefiner.getInstance();
                if (refiner != null) {
                    refiner.useGlobalRulesForDomains(originArray);
                }
            }
        }

        private void populateOrigins(Map<String, Site> sites) {
            clear();

            // We can now simply populate our array with Site instances
            Set<Map.Entry<String, Site>> elements = sites.entrySet();
            Iterator<Map.Entry<String, Site>> entryIterator = elements.iterator();
            while (entryIterator.hasNext()) {
                Map.Entry<String, Site> entry = entryIterator.next();
                Site site = entry.getValue();
                add(site);
            }

            notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            final TextView title;
            final TextView subtitle;
            final ImageView icon;

            if (convertView == null) {
                view = mInflater.inflate(mResource, parent, false);
            } else {
                view = convertView;
            }

            title = (TextView) view.findViewById(R.id.title);
            subtitle = (TextView) view.findViewById(R.id.subtitle);
            icon = (ImageView) view.findViewById(R.id.icon);

            Site site = getItem(position);
            title.setText(site.getPrettyTitle());
            String subtitleText = site.getPrettyOrigin();
            if (subtitleText != null) {
                title.setMaxLines(1);
                title.setSingleLine(true);
                subtitle.setVisibility(View.VISIBLE);
                subtitle.setText(subtitleText);
            } else {
                subtitle.setVisibility(View.GONE);
                title.setMaxLines(2);
                title.setSingleLine(false);
            }

            icon.setVisibility(View.VISIBLE);
            Bitmap bmp = site.getIcon();
            if (bmp == null) {
                bmp = mDefaultIcon;
            }
            icon.setImageBitmap(bmp);
            // We set the site as the view's tag,
            // so that we can get it in onItemClick()
            view.setTag(site);

            return view;
        }

        public void onItemClick(AdapterView<?> parent,
                                View view,
                                int position,
                                long id) {
            Site site = (Site) view.getTag();
            Activity activity = getActivity();
            if (activity != null) {
                Bundle args = new Bundle();
                args.putString(SiteSpecificPreferencesFragment.EXTRA_ORIGIN, site.getOrigin());

                FragmentManager fragmentManager = activity.getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                Fragment newFragment = new SiteSpecificPreferencesFragment();
                newFragment.setArguments(args);
                fragmentTransaction.replace(getId(), newFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.swe_website_settings, container, false);
        View new_site = view.findViewById(R.id.add_new_site);
        new_site.setVisibility(View.VISIBLE);
        new_site.setOnClickListener(this);
        View clear = view.findViewById(R.id.clear_all_button);
        clear.setVisibility(View.VISIBLE);
        clear.setOnClickListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mAdapter = new SiteAdapter(getActivity(), R.layout.website_settings_row);
        getListView().setAdapter(mAdapter);
        getListView().setOnItemClickListener(mAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        mAdapter.askForOrigins();
        ActionBar bar = getActivity().getActionBar();
        if (bar != null) {
            bar.setTitle(R.string.pref_extras_website_settings);
            bar.setDisplayHomeAsUpEnabled(false);
            bar.setHomeButtonEnabled(false);
            bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.accent)));
        }
    }

    private void finish() {
        Activity activity = getActivity();
        if (activity != null) {
            getActivity().getFragmentManager().popBackStack();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.clear_all_button:
            // Show the prompt to clear all origins of their data and geolocation permissions.
            new AlertDialog.Builder(getActivity())
                .setMessage(R.string.website_settings_clear_all_dialog_message)
                .setPositiveButton(R.string.website_settings_clear_all_dialog_ok_button,
                        new AlertDialog.OnClickListener() {
                            public void onClick(DialogInterface dlg, int which) {
                                mAdapter.deleteAllOrigins();
                                WebStorage.getInstance().deleteAllData();
                                if (GeolocationPermissions.isIncognitoCreated()) {
                                    GeolocationPermissions.getIncognitoInstance().clearAll();
                                }
                                WebStorageSizeManager.resetLastOutOfSpaceNotificationTime();
                                mAdapter.askForOrigins();
                                finish();
                            }
                        })
                .setNegativeButton(R.string.website_settings_clear_all_dialog_cancel_button, null)
                .setIconAttribute(android.R.attr.alertDialogIcon)
                .show();
            break;
        case R.id.add_new_site:
            final EditText input = new EditText(getActivity());
            new AlertDialog.Builder(getActivity())
                    .setTitle(R.string.website_settings_add_origin)
                    .setMessage(R.string.pref_security_origin_name)
                    .setView(input)
                    .setPositiveButton(R.string.pref_security_add,
                            new AlertDialog.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    String origin = input.getText().toString();
                                    Bundle args = new Bundle();
                                    args.putString(SiteSpecificPreferencesFragment.EXTRA_SITE,
                                            origin);

                                    FragmentTransaction fragmentTransaction =
                                            getActivity().getFragmentManager().beginTransaction();

                                    Fragment newFragment = new SiteSpecificPreferencesFragment();
                                    newFragment.setArguments(args);
                                    fragmentTransaction.replace(getId(), newFragment);
                                    fragmentTransaction.addToBackStack(null);
                                    fragmentTransaction.commit();
                                }})
                    .setNegativeButton(R.string.pref_security_cancel, null)
                    .show();

            break;
        }
    }
}