BrowserBookmarksPage.java revision 4aff3aae62553f308612d7c6f284ca84aa33e026
1/*
2 * Copyright (C) 2006 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.browser;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.content.DialogInterface;
22import android.content.Intent;
23import android.content.SharedPreferences;
24import android.content.SharedPreferences.Editor;
25import android.graphics.Bitmap;
26import android.graphics.BitmapFactory;
27import android.graphics.Canvas;
28import android.graphics.Color;
29import android.graphics.Paint;
30import android.graphics.Path;
31import android.graphics.PorterDuff;
32import android.graphics.PorterDuffXfermode;
33import android.graphics.RectF;
34import android.net.Uri;
35import android.os.Bundle;
36import android.os.Handler;
37import android.os.Message;
38import android.os.ServiceManager;
39import android.provider.Browser;
40import android.text.IClipboard;
41import android.util.Log;
42import android.view.ContextMenu;
43import android.view.KeyEvent;
44import android.view.LayoutInflater;
45import android.view.Menu;
46import android.view.MenuInflater;
47import android.view.MenuItem;
48import android.view.View;
49import android.view.ViewGroup;
50import android.view.ViewGroup.LayoutParams;
51import android.view.ViewStub;
52import android.view.ContextMenu.ContextMenuInfo;
53import android.widget.AdapterView;
54import android.widget.GridView;
55import android.widget.ListView;
56import android.widget.Toast;
57
58/*package*/ enum BookmarkViewMode { NONE, GRID, LIST }
59/**
60 *  View showing the user's bookmarks in the browser.
61 */
62public class BrowserBookmarksPage extends Activity implements
63        View.OnCreateContextMenuListener {
64
65    private BookmarkViewMode        mViewMode = BookmarkViewMode.NONE;
66    private GridView                mGridPage;
67    private View                    mVerticalList;
68    private BrowserBookmarksAdapter mBookmarksAdapter;
69    private static final int        BOOKMARKS_SAVE = 1;
70    private boolean                 mDisableNewWindow;
71    private BookmarkItem            mContextHeader;
72    private AddNewBookmark          mAddHeader;
73    private boolean                 mCanceled = false;
74    private boolean                 mCreateShortcut;
75    private boolean                 mMostVisited;
76    private View                    mEmptyView;
77    // XXX: There is no public string defining this intent so if Home changes
78    // the value, we have to update this string.
79    private static final String     INSTALL_SHORTCUT =
80            "com.android.launcher.action.INSTALL_SHORTCUT";
81
82    private final static String LOGTAG = "browser";
83    private final static String PREF_BOOKMARK_VIEW_MODE = "pref_bookmark_view_mode";
84    private final static String PREF_MOST_VISITED_VIEW_MODE = "pref_most_visited_view_mode";
85
86    @Override
87    public boolean onContextItemSelected(MenuItem item) {
88        // It is possible that the view has been canceled when we get to
89        // this point as back has a higher priority
90        if (mCanceled) {
91            return true;
92        }
93        AdapterView.AdapterContextMenuInfo i =
94            (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
95        // If we have no menu info, we can't tell which item was selected.
96        if (i == null) {
97            return true;
98        }
99
100        switch (item.getItemId()) {
101        case R.id.new_context_menu_id:
102            saveCurrentPage();
103            break;
104        case R.id.open_context_menu_id:
105            loadUrl(i.position);
106            break;
107        case R.id.edit_context_menu_id:
108            editBookmark(i.position);
109            break;
110        case R.id.shortcut_context_menu_id:
111            final Intent send = createShortcutIntent(i.position);
112            send.setAction(INSTALL_SHORTCUT);
113            sendBroadcast(send);
114            break;
115        case R.id.delete_context_menu_id:
116            if (mMostVisited) {
117                Browser.deleteFromHistory(getContentResolver(),
118                        getUrl(i.position));
119                refreshList();
120            } else {
121                displayRemoveBookmarkDialog(i.position);
122            }
123            break;
124        case R.id.new_window_context_menu_id:
125            openInNewWindow(i.position);
126            break;
127        case R.id.share_link_context_menu_id:
128            Browser.sendString(BrowserBookmarksPage.this, getUrl(i.position),
129                    getText(R.string.choosertitle_sharevia).toString());
130            break;
131        case R.id.copy_url_context_menu_id:
132            copy(getUrl(i.position));
133            break;
134        case R.id.homepage_context_menu_id:
135            BrowserSettings.getInstance().setHomePage(this,
136                    getUrl(i.position));
137            Toast.makeText(this, R.string.homepage_set,
138                    Toast.LENGTH_LONG).show();
139            break;
140        // Only for the Most visited page
141        case R.id.save_to_bookmarks_menu_id:
142            boolean isBookmark;
143            String name;
144            String url;
145            if (mViewMode == BookmarkViewMode.GRID) {
146                isBookmark = mBookmarksAdapter.getIsBookmark(i.position);
147                name = mBookmarksAdapter.getTitle(i.position);
148                url = mBookmarksAdapter.getUrl(i.position);
149            } else {
150                HistoryItem historyItem = ((HistoryItem) i.targetView);
151                isBookmark = historyItem.isBookmark();
152                name = historyItem.getName();
153                url = historyItem.getUrl();
154            }
155            // If the site is bookmarked, the item becomes remove from
156            // bookmarks.
157            if (isBookmark) {
158                Bookmarks.removeFromBookmarks(this, getContentResolver(), url, name);
159            } else {
160                Browser.saveBookmark(this, name, url);
161            }
162            break;
163        default:
164            return super.onContextItemSelected(item);
165        }
166        return true;
167    }
168
169    @Override
170    public void onCreateContextMenu(ContextMenu menu, View v,
171                ContextMenuInfo menuInfo) {
172            AdapterView.AdapterContextMenuInfo i =
173                    (AdapterView.AdapterContextMenuInfo) menuInfo;
174
175            MenuInflater inflater = getMenuInflater();
176            if (mMostVisited) {
177                inflater.inflate(R.menu.historycontext, menu);
178            } else {
179                inflater.inflate(R.menu.bookmarkscontext, menu);
180            }
181
182            if (0 == i.position && !mMostVisited) {
183                menu.setGroupVisible(R.id.CONTEXT_MENU, false);
184                if (mAddHeader == null) {
185                    mAddHeader = new AddNewBookmark(BrowserBookmarksPage.this);
186                } else if (mAddHeader.getParent() != null) {
187                    ((ViewGroup) mAddHeader.getParent()).
188                            removeView(mAddHeader);
189                }
190                mAddHeader.setUrl(getIntent().getStringExtra("url"));
191                menu.setHeaderView(mAddHeader);
192                return;
193            }
194            if (mMostVisited) {
195                if ((mViewMode == BookmarkViewMode.LIST
196                        && ((HistoryItem) i.targetView).isBookmark())
197                        || mBookmarksAdapter.getIsBookmark(i.position)) {
198                    MenuItem item = menu.findItem(
199                            R.id.save_to_bookmarks_menu_id);
200                    item.setTitle(R.string.remove_from_bookmarks);
201                }
202            } else {
203                // The historycontext menu has no ADD_MENU group.
204                menu.setGroupVisible(R.id.ADD_MENU, false);
205            }
206            if (mDisableNewWindow) {
207                menu.findItem(R.id.new_window_context_menu_id).setVisible(
208                        false);
209            }
210            if (mContextHeader == null) {
211                mContextHeader = new BookmarkItem(BrowserBookmarksPage.this);
212            } else if (mContextHeader.getParent() != null) {
213                ((ViewGroup) mContextHeader.getParent()).
214                        removeView(mContextHeader);
215            }
216            if (mViewMode == BookmarkViewMode.GRID) {
217                mBookmarksAdapter.populateBookmarkItem(mContextHeader,
218                        i.position);
219            } else {
220                BookmarkItem b = (BookmarkItem) i.targetView;
221                b.copyTo(mContextHeader);
222            }
223            menu.setHeaderView(mContextHeader);
224        }
225
226    /**
227     *  Create a new BrowserBookmarksPage.
228     */
229    @Override
230    protected void onCreate(Bundle icicle) {
231        super.onCreate(icicle);
232
233        if (Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction())) {
234            mCreateShortcut = true;
235        }
236        mDisableNewWindow = getIntent().getBooleanExtra("disable_new_window",
237                false);
238        mMostVisited = getIntent().getBooleanExtra("mostVisited", false);
239
240        if (mCreateShortcut) {
241            setTitle(R.string.browser_bookmarks_page_bookmarks_text);
242        }
243        mBookmarksAdapter = new BrowserBookmarksAdapter(this,
244                        getIntent().getStringExtra("url"),
245                        getIntent().getStringExtra("title"),
246                        (Bitmap) getIntent().getParcelableExtra("thumbnail"),
247                        mCreateShortcut,
248                        mMostVisited);
249
250        setContentView(R.layout.empty_history);
251        mEmptyView = findViewById(R.id.empty_view);
252        mEmptyView.setVisibility(View.GONE);
253
254        SharedPreferences p = getPreferences(MODE_PRIVATE);
255
256        // See if the user has set a preference for the view mode of their
257        // bookmarks. Otherwise default to grid mode.
258        BookmarkViewMode preference = BookmarkViewMode.NONE;
259        if (mMostVisited) {
260            preference = BookmarkViewMode.values()[p.getInt(
261                    PREF_MOST_VISITED_VIEW_MODE,
262                    BookmarkViewMode.GRID.ordinal())];
263        } else {
264            preference = BookmarkViewMode.values()[p.getInt(
265                    PREF_BOOKMARK_VIEW_MODE, BookmarkViewMode.GRID.ordinal())];
266        }
267        switchViewMode(preference);
268    }
269
270    /**
271     *  Set the ContentView to be either the grid of thumbnails or the vertical
272     *  list.
273     */
274    private void switchViewMode(BookmarkViewMode gridMode) {
275        if (mViewMode == gridMode) {
276            return;
277        }
278
279        mViewMode = gridMode;
280
281        // Update the preferences to make the new view mode sticky.
282        Editor ed = getPreferences(MODE_PRIVATE).edit();
283        if (mMostVisited) {
284            ed.putInt(PREF_MOST_VISITED_VIEW_MODE, mViewMode.ordinal());
285        } else {
286            ed.putInt(PREF_BOOKMARK_VIEW_MODE, mViewMode.ordinal());
287        }
288        ed.commit();
289
290        mBookmarksAdapter.switchViewMode(gridMode);
291        if (mViewMode == BookmarkViewMode.GRID) {
292            if (mGridPage == null) {
293                mGridPage = new GridView(this);
294                mGridPage.setAdapter(mBookmarksAdapter);
295                mGridPage.setOnItemClickListener(mListener);
296                mGridPage.setNumColumns(GridView.AUTO_FIT);
297                mGridPage.setColumnWidth(
298                        BrowserActivity.getDesiredThumbnailWidth(this));
299                mGridPage.setFocusable(true);
300                mGridPage.setFocusableInTouchMode(true);
301                mGridPage.setSelector(android.R.drawable.gallery_thumb);
302                float density = getResources().getDisplayMetrics().density;
303                mGridPage.setVerticalSpacing((int) (14 * density));
304                mGridPage.setHorizontalSpacing((int) (8 * density));
305                mGridPage.setStretchMode(GridView.STRETCH_SPACING);
306                mGridPage.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
307                mGridPage.setDrawSelectorOnTop(true);
308                if (mMostVisited) {
309                    mGridPage.setEmptyView(mEmptyView);
310                }
311                if (!mCreateShortcut) {
312                    mGridPage.setOnCreateContextMenuListener(this);
313                }
314            }
315            addContentView(mGridPage, FULL_SCREEN_PARAMS);
316            if (mVerticalList != null) {
317                ViewGroup parent = (ViewGroup) mVerticalList.getParent();
318                if (parent != null) {
319                    parent.removeView(mVerticalList);
320                }
321            }
322        } else {
323            if (null == mVerticalList) {
324                ListView listView = new ListView(this);
325                listView.setAdapter(mBookmarksAdapter);
326                listView.setDrawSelectorOnTop(false);
327                listView.setVerticalScrollBarEnabled(true);
328                listView.setOnItemClickListener(mListener);
329                if (mMostVisited) {
330                    listView.setEmptyView(mEmptyView);
331                }
332                if (!mCreateShortcut) {
333                    listView.setOnCreateContextMenuListener(this);
334                }
335                mVerticalList = listView;
336            }
337            addContentView(mVerticalList, FULL_SCREEN_PARAMS);
338            if (mGridPage != null) {
339                ViewGroup parent = (ViewGroup) mGridPage.getParent();
340                if (parent != null) {
341                    parent.removeView(mGridPage);
342                }
343            }
344        }
345    }
346
347    private static final ViewGroup.LayoutParams FULL_SCREEN_PARAMS
348            = new ViewGroup.LayoutParams(
349            ViewGroup.LayoutParams.FILL_PARENT,
350            ViewGroup.LayoutParams.FILL_PARENT);
351
352    private static final int SAVE_CURRENT_PAGE = 1000;
353    private final Handler mHandler = new Handler() {
354        @Override
355        public void handleMessage(Message msg) {
356            if (msg.what == SAVE_CURRENT_PAGE) {
357                saveCurrentPage();
358            }
359        }
360    };
361
362    private AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() {
363        public void onItemClick(AdapterView parent, View v, int position, long id) {
364            // It is possible that the view has been canceled when we get to
365            // this point as back has a higher priority
366            if (mCanceled) {
367                android.util.Log.e(LOGTAG, "item clicked when dismissing");
368                return;
369            }
370            if (!mCreateShortcut) {
371                if (0 == position && !mMostVisited) {
372                    // XXX: Work-around for a framework issue.
373                    mHandler.sendEmptyMessage(SAVE_CURRENT_PAGE);
374                } else {
375                    loadUrl(position);
376                }
377            } else {
378                final Intent intent = createShortcutIntent(position);
379                setResultToParent(RESULT_OK, intent);
380                finish();
381            }
382        }
383    };
384
385    private Intent createShortcutIntent(int position) {
386        String url = getUrl(position);
387        String title = getBookmarkTitle(position);
388        Bitmap touchIcon = getTouchIcon(position);
389
390        final Intent i = new Intent();
391        final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW,
392                Uri.parse(url));
393        long urlHash = url.hashCode();
394        long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
395        shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID,
396                Long.toString(uniqueId));
397        i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
398        i.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
399        // Use the apple-touch-icon if available
400        if (touchIcon != null) {
401            // Make a copy so we can modify the pixels.
402            Bitmap copy = touchIcon.copy(Bitmap.Config.ARGB_8888, true);
403            Canvas canvas = new Canvas(copy);
404
405            // Construct a path from a round rect. This will allow drawing with
406            // an inverse fill so we can punch a hole using the round rect.
407            Path path = new Path();
408            path.setFillType(Path.FillType.INVERSE_WINDING);
409            RectF rect = new RectF(0, 0, touchIcon.getWidth(),
410                    touchIcon.getHeight());
411            rect.inset(1, 1);
412            path.addRoundRect(rect, 8f, 8f, Path.Direction.CW);
413
414            // Construct a paint that clears the outside of the rectangle and
415            // draw.
416            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
417            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
418            canvas.drawPath(path, paint);
419
420            i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
421        } else {
422            Bitmap favicon = getFavicon(position);
423            if (favicon == null) {
424                i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
425                        Intent.ShortcutIconResource.fromContext(
426                                BrowserBookmarksPage.this,
427                                R.drawable.ic_launcher_shortcut_browser_bookmark));
428            } else {
429                Bitmap icon = BitmapFactory.decodeResource(getResources(),
430                        R.drawable.ic_launcher_shortcut_browser_bookmark);
431
432                // Make a copy of the regular icon so we can modify the pixels.
433                Bitmap copy = icon.copy(Bitmap.Config.ARGB_8888, true);
434                Canvas canvas = new Canvas(copy);
435
436                // Make a Paint for the white background rectangle and for
437                // filtering the favicon.
438                Paint p = new Paint(Paint.ANTI_ALIAS_FLAG
439                        | Paint.FILTER_BITMAP_FLAG);
440                p.setStyle(Paint.Style.FILL_AND_STROKE);
441                p.setColor(Color.WHITE);
442
443                float density = getResources().getDisplayMetrics().density;
444                // Create a rectangle that is slightly wider than the favicon
445                final float iconSize = 16 * density; // 16x16 favicon
446                final float padding = 2; // white padding around icon
447                final float rectSize = iconSize + 2 * padding;
448                final float y = icon.getHeight() - rectSize;
449                RectF r = new RectF(0, y, rectSize, y + rectSize);
450
451                // Draw a white rounded rectangle behind the favicon
452                canvas.drawRoundRect(r, 2, 2, p);
453
454                // Draw the favicon in the same rectangle as the rounded
455                // rectangle but inset by the padding
456                // (results in a 16x16 favicon).
457                r.inset(padding, padding);
458                canvas.drawBitmap(favicon, null, r, p);
459                i.putExtra(Intent.EXTRA_SHORTCUT_ICON, copy);
460            }
461        }
462        // Do not allow duplicate items
463        i.putExtra("duplicate", false);
464        return i;
465    }
466
467    private void saveCurrentPage() {
468        Intent i = new Intent(BrowserBookmarksPage.this,
469                AddBookmarkPage.class);
470        i.putExtras(getIntent());
471        startActivityForResult(i, BOOKMARKS_SAVE);
472    }
473
474    private void loadUrl(int position) {
475        Intent intent = (new Intent()).setAction(getUrl(position));
476        setResultToParent(RESULT_OK, intent);
477        finish();
478    }
479
480    @Override
481    public boolean onCreateOptionsMenu(Menu menu) {
482        boolean result = super.onCreateOptionsMenu(menu);
483        if (!mCreateShortcut) {
484            MenuInflater inflater = getMenuInflater();
485            inflater.inflate(R.menu.bookmarks, menu);
486            // Most visited page does not have an option to bookmark the last
487            // viewed page.
488            menu.findItem(R.id.new_context_menu_id).setVisible(!mMostVisited);
489            return true;
490        }
491        return result;
492    }
493
494    @Override
495    public boolean onPrepareOptionsMenu(Menu menu) {
496        boolean result = super.onPrepareOptionsMenu(menu);
497        if (mCreateShortcut || mBookmarksAdapter.getCount() == 0) {
498            // No need to show the menu if there are no items.
499            return result;
500        }
501        MenuItem switchItem = menu.findItem(R.id.switch_mode_menu_id);
502        int titleResId;
503        int iconResId;
504        if (mViewMode == BookmarkViewMode.GRID) {
505            titleResId = R.string.switch_to_list;
506            iconResId = R.drawable.ic_menu_list;
507        } else {
508            titleResId = R.string.switch_to_thumbnails;
509            iconResId = R.drawable.ic_menu_thumbnail;
510        }
511        switchItem.setTitle(titleResId);
512        switchItem.setIcon(iconResId);
513        return true;
514    }
515
516    @Override
517    public boolean onOptionsItemSelected(MenuItem item) {
518        switch (item.getItemId()) {
519        case R.id.new_context_menu_id:
520            saveCurrentPage();
521            break;
522
523        case R.id.switch_mode_menu_id:
524            if (mViewMode == BookmarkViewMode.GRID) {
525                switchViewMode(BookmarkViewMode.LIST);
526            } else {
527                switchViewMode(BookmarkViewMode.GRID);
528            }
529            break;
530
531        default:
532            return super.onOptionsItemSelected(item);
533        }
534        return true;
535    }
536
537    private void openInNewWindow(int position) {
538        Bundle b = new Bundle();
539        b.putBoolean("new_window", true);
540        setResultToParent(RESULT_OK,
541                (new Intent()).setAction(getUrl(position)).putExtras(b));
542
543        finish();
544    }
545
546
547    private void editBookmark(int position) {
548        Intent intent = new Intent(BrowserBookmarksPage.this,
549            AddBookmarkPage.class);
550        intent.putExtra("bookmark", getRow(position));
551        startActivityForResult(intent, BOOKMARKS_SAVE);
552    }
553
554    @Override
555    protected void onActivityResult(int requestCode, int resultCode,
556                                    Intent data) {
557        switch(requestCode) {
558            case BOOKMARKS_SAVE:
559                if (resultCode == RESULT_OK) {
560                    Bundle extras;
561                    if (data != null && (extras = data.getExtras()) != null) {
562                        // If there are extras, then we need to save
563                        // the edited bookmark. This is done in updateRow()
564                        String title = extras.getString("title");
565                        String url = extras.getString("url");
566                        if (title != null && url != null) {
567                            mBookmarksAdapter.updateRow(extras);
568                        }
569                    } else {
570                        // extras == null then a new bookmark was added to
571                        // the database.
572                        refreshList();
573                    }
574                }
575                break;
576            default:
577                break;
578        }
579    }
580
581    private void displayRemoveBookmarkDialog(int position) {
582        // Put up a dialog asking if the user really wants to
583        // delete the bookmark
584        final int deletePos = position;
585        new AlertDialog.Builder(this)
586                .setTitle(R.string.delete_bookmark)
587                .setIcon(android.R.drawable.ic_dialog_alert)
588                .setMessage(getText(R.string.delete_bookmark_warning).toString().replace(
589                        "%s", getBookmarkTitle(deletePos)))
590                .setPositiveButton(R.string.ok,
591                        new DialogInterface.OnClickListener() {
592                            public void onClick(DialogInterface dialog, int whichButton) {
593                                deleteBookmark(deletePos);
594                            }
595                        })
596                .setNegativeButton(R.string.cancel, null)
597                .show();
598    }
599
600    /**
601     *  Refresh the shown list after the database has changed.
602     */
603    private void refreshList() {
604        mBookmarksAdapter.refreshList();
605    }
606
607    /**
608     *  Return a hashmap representing the currently highlighted row.
609     */
610    public Bundle getRow(int position) {
611        return mBookmarksAdapter.getRow(position);
612    }
613
614    /**
615     *  Return the url of the currently highlighted row.
616     */
617    public String getUrl(int position) {
618        return mBookmarksAdapter.getUrl(position);
619    }
620
621    /**
622     * Return the favicon of the currently highlighted row.
623     */
624    public Bitmap getFavicon(int position) {
625        return mBookmarksAdapter.getFavicon(position);
626    }
627
628    private Bitmap getTouchIcon(int position) {
629        return mBookmarksAdapter.getTouchIcon(position);
630    }
631
632    private void copy(CharSequence text) {
633        try {
634            IClipboard clip = IClipboard.Stub.asInterface(ServiceManager.getService("clipboard"));
635            if (clip != null) {
636                clip.setClipboardText(text);
637            }
638        } catch (android.os.RemoteException e) {
639            Log.e(LOGTAG, "Copy failed", e);
640        }
641    }
642
643    public String getBookmarkTitle(int position) {
644        return mBookmarksAdapter.getTitle(position);
645    }
646
647    /**
648     *  Delete the currently highlighted row.
649     */
650    public void deleteBookmark(int position) {
651        mBookmarksAdapter.deleteRow(position);
652    }
653
654    @Override
655    public void onBackPressed() {
656        setResultToParent(RESULT_CANCELED, null);
657        mCanceled = true;
658        super.onBackPressed();
659    }
660
661    // This Activity is generally a sub-Activity of CombinedHistoryActivity. In
662    // that situation, we need to pass our result code up to our parent.
663    // However, if someone calls this Activity directly, then this has no
664    // parent, and it needs to set it on itself.
665    private void setResultToParent(int resultCode, Intent data) {
666        Activity a = getParent() == null ? this : getParent();
667        a.setResult(resultCode, data);
668    }
669}
670