summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/overlays/service/sharemethods/ShareMethod.java
blob: 768176d6356b0246b6e6006c820e876b0e2a8389 (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
/*This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.overlays.service.sharemethods;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import org.mozilla.gecko.overlays.service.ShareData;

/**
 * Represents a method of sharing a URL/title. Add a bookmark? Send to a device? Add to reading list?
 */
public abstract class ShareMethod {
    protected final Context context;

    public ShareMethod(Context aContext) {
        context = aContext;
    }

    /**
     * Perform a share for the given title/URL combination. Called on the background thread by the
     * handler service when a request is made. The "extra" parameter is provided should a ShareMethod
     * desire to handle the share differently based on some additional parameters.
     *
     * @param title The page title for the page being shared. May be null if none can be found.
     * @param url The URL of the page to be shared. Never null.
     * @param extra A Parcelable of ShareMethod-specific parameters that may be provided by the
     *              caller. Generally null, but this field may be used to provide extra input to
     *              the ShareMethod (such as the device to share to in the case of SendTab).
     * @return true if the attempt to share was a success. False in the event of an error.
     */
    public abstract Result handle(ShareData shareData);

    /**
     * Enum representing the possible results of performing a share.
     */
    public static enum Result {
        // Victory!
        SUCCESS,

        // Failure, but retrying the same action again might lead to success.
        TRANSIENT_FAILURE,

        // Failure, and you're not going to succeed until you reinitialise the ShareMethod (ie.
        // until you repeat the entire share action). Examples include broken Sync accounts, or
        // Sync accounts with no valid target devices (so the only way to fix this is to add some
        // and try again: pushing a retry button isn't sane).
        PERMANENT_FAILURE
    }

    /**
     * Enum representing types of ShareMethod. Parcelable so it may be efficiently used in Intents.
     */
    public static enum Type implements Parcelable {
        ADD_BOOKMARK,
        SEND_TAB;

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(final Parcel dest, final int flags) {
            dest.writeInt(ordinal());
        }

        public static final Creator<Type> CREATOR = new Creator<Type>() {
            @Override
            public Type createFromParcel(final Parcel source) {
                return Type.values()[source.readInt()];
            }

            @Override
            public Type[] newArray(final int size) {
                return new Type[size];
            }
        };
    }
}