to top
ActionBarCompat-ShareActionProvider / src / com.example.android.actionbarcompat.shareactionprovider / content /

ContentItem.java

1
/*
2
 * Copyright (C) 2013 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
 
17
package com.example.android.actionbarcompat.shareactionprovider.content;
18
 
19
import android.content.Context;
20
import android.content.Intent;
21
import android.net.Uri;
22
import android.text.TextUtils;
23
 
24
/**
25
 * This class encapsulates a content item. Referencing the content's type, and the differing way
26
 * to reference the content (asset URI or resource id).
27
 */
28
public class ContentItem {
29
    // Used to signify an image content type
30
    public static final int CONTENT_TYPE_IMAGE = 0;
31
    // Used to signify a text/string content type
32
    public static final int CONTENT_TYPE_TEXT = 1;
33
 
34
    public final int contentType;
35
    public final int contentResourceId;
36
    public final String contentAssetFilePath;
37
 
38
    /**
39
     * Creates a ContentItem with the specified type, referencing a resource id.
40
     *
41
     * @param type - One of {@link #CONTENT_TYPE_IMAGE} or {@link #CONTENT_TYPE_TEXT}
42
     * @param resourceId - Resource ID to use for this item's content
43
     */
44
    public ContentItem(int type, int resourceId) {
45
        contentType = type;
46
        contentResourceId = resourceId;
47
        contentAssetFilePath = null;
48
    }
49
 
50
    /**
51
     * Creates a ContentItem with the specified type, referencing an asset file path.
52
     *
53
     * @param type - One of {@link #CONTENT_TYPE_IMAGE} or {@link #CONTENT_TYPE_TEXT}
54
     * @param assetFilePath - File path from the application's asset for this item's content
55
     */
56
    public ContentItem(int type, String assetFilePath) {
57
        contentType = type;
58
        contentAssetFilePath = assetFilePath;
59
        contentResourceId = 0;
60
    }
61
 
62
    /**
63
     * @return Uri to the content
64
     */
65
    public Uri getContentUri() {
66
        if (!TextUtils.isEmpty(contentAssetFilePath)) {
67
            // If this content has an asset, then return a AssetProvider Uri
68
            return Uri.parse("content://" + AssetProvider.CONTENT_URI + "/" + contentAssetFilePath);
69
        } else {
70
            return null;
71
        }
72
    }
73
 
74
    /**
75
     * Returns an {@link android.content.Intent} which can be used to share this item's content with other
76
     * applications.
77
     *
78
     * @param context - Context to be used for fetching resources if needed
79
     * @return Intent to be given to a ShareActionProvider.
80
     */
81
    public Intent getShareIntent(Context context) {
82
        Intent intent = new Intent(Intent.ACTION_SEND);
83
 
84
        switch (contentType) {
85
            case CONTENT_TYPE_IMAGE:
86
                intent.setType("image/jpg");
87
                // Bundle the asset content uri as the EXTRA_STREAM uri
88
                intent.putExtra(Intent.EXTRA_STREAM, getContentUri());
89
                break;
90
 
91
            case CONTENT_TYPE_TEXT:
92
                intent.setType("text/plain");
93
                // Get the string resource and bundle it as an intent extra
94
                intent.putExtra(Intent.EXTRA_TEXT, context.getString(contentResourceId));
95
                break;
96
        }
97
 
98
        return intent;
99
    }
100
 
101
}