/*
 * Copyright 2014 Uwe Trottmann
 *
 * 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.battlelancer.seriesguide.api;

import android.content.Intent;

public class Action {

    private String mTitle;
    private Intent mViewIntent;

    private Action(String title, Intent viewIntent) {
        mTitle = title;
        mViewIntent = viewIntent;
    }

    /**
     * Returns the user-visible title for this action.
     */
    public String getTitle() {
        return mTitle;
    }

    /**
     * Returns the activity {@link Intent} that will be started when the user clicks on the button
     * for this action.
     */
    public Intent getViewIntent() {
        return mViewIntent;
    }

    public static class Builder {

        private String mTitle;
        private Intent mViewIntent;

        /**
         * Sets the user-visible title of the button for this action, returns a builder
         * instance to add further properties.
         */
        public Builder(String title) {
            if (title == null || title.length() == 0) {
                throw new IllegalArgumentException("Title may not be null or empty.");
            }
            mTitle = title;
        }

        /**
         * Sets the (optional) activity {@link Intent} that will be {@linkplain
         * android.content.Context#startActivity(Intent)
         * started} when the user clicks the button for the action.<br/>
         * <br/>
         * The activity that this intent resolves to must have <code>android:exported</code> set
         * to <code>true</code>.
         */
        public Builder viewIntent(Intent viewIntent) {
            mViewIntent = viewIntent;
            return this;
        }

        public Action build() {
            return new Action(mTitle, mViewIntent);
        }
    }
}

@@ -670,30 +670,299 @@
 nt;%0A
-%0Apublic class Action %7B
+import android.os.Bundle;%0Aimport android.text.TextUtils;%0Aimport java.net.URISyntaxException;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Apublic class Action %7B%0A    private static final String KEY_TITLE = %22title%22;%0A    private static final String KEY_VIEW_INTENT = %22viewIntent%22;
 %0A%0A  
@@ -2586,11 +2586,2154 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Serializes this %7B@link Action%7D object to a %7B@link android.os.Bundle%7D representation.%0A     */%0A    public Bundle toBundle() %7B%0A        Bundle bundle = new Bundle();%0A        bundle.putString(KEY_TITLE, mTitle);%0A        bundle.putString(KEY_VIEW_INTENT, (mViewIntent != null)%0A                ? mViewIntent.toUri(Intent.URI_INTENT_SCHEME) : null);%0A        return bundle;%0A    %7D%0A%0A    /**%0A     * Deserializes a %7B@link JSONObject%7D into an %7B@link Bundle%7D object.%0A     */%0A    public static Action fromBundle(Bundle bundle) %7B%0A        String title = bundle.getString(KEY_TITLE);%0A        if (TextUtils.isEmpty(title)) %7B%0A            return null;%0A        %7D%0A%0A        Builder builder = new Builder(title);%0A%0A        try %7B%0A            String viewIntent = bundle.getString(KEY_VIEW_INTENT);%0A            if (!TextUtils.isEmpty(viewIntent)) %7B%0A                builder.viewIntent(Intent.parseUri(viewIntent, Intent.URI_INTENT_SCHEME));%0A            %7D%0A        %7D catch (URISyntaxException ignored) %7B%0A        %7D%0A%0A        return builder.build();%0A    %7D%0A%0A    /**%0A     * Serializes this %7B@link Action%7D object to a %7B@link org.json.JSONObject%7D representation.%0A     */%0A    public JSONObject toJson() throws JSONException %7B%0A        JSONObject jsonObject = new JSONObject();%0A        jsonObject.put(KEY_TITLE, mTitle);%0A        jsonObject.put(KEY_VIEW_INTENT, (mViewIntent != null)%0A                ? mViewIntent.toUri(Intent.URI_INTENT_SCHEME) : null);%0A        return jsonObject;%0A    %7D%0A%0A    /**%0A     * Deserializes a %7B@link JSONObject%7D into an %7B@link Action%7D object.%0A     */%0A    public static Action fromJson(JSONObject jsonObject) throws JSONException %7B%0A        String title = jsonObject.optString(KEY_TITLE);%0A        if (TextUtils.isEmpty(title)) %7B%0A            return null;%0A        %7D%0A        Builder builder = new Builder(title);%0A%0A        try %7B%0A            String viewIntent = jsonObject.optString(KEY_VIEW_INTENT);%0A            if (!TextUtils.isEmpty(viewIntent)) %7B%0A                builder.viewIntent(Intent.parseUri(viewIntent, Intent.URI_INTENT_SCHEME));%0A            %7D%0A        %7D catch (URISyntaxException ignored) %7B%0A        %7D%0A%0A        return builder.build();%0A    %7D
 %0A%7D%0A

Support Bundle and JSON serialization for Action.