/*
 * Copyright (C) 2016 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.example.android.implicitintents;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     * This method is called when the Open Website button is clicked. It will open the website
     * specified by the URL represented by the variable urlAsString using implicit Intents.
     *
     * @param v Button that was clicked.
     */
    public void onClickOpenWebpageButton(View v) {
        String urlAsString = "http://www.udacity.com";
        openWebPage(urlAsString);
    }

    /**
     * This method is called when the Open Location in Map button is clicked. It will open the
     * a map to the location represented by the variable addressString using implicit Intents.
     *
     * @param v Button that was clicked.
     */
    public void onClickOpenAddressButton(View v) {
        // TODO (5) Store an address in a String

        // TODO (6) Use Uri.parse with the appropriate scheme and query to form the Uri for the address

        // TODO (7) Replace the Toast with a call to showMap, passing in the Uri from the previous step
        Toast.makeText(this, "TODO: Open a map when this button is clicked", Toast.LENGTH_SHORT).show();
    }

    /**
     * This method is called when the Share Text Content button is clicked. It will simply share
     * the text contained within the String textThatYouWantToShare.
     *
     * @param v Button that was clicked.
     */
    public void onClickShareTextButton(View v) {
        Toast.makeText(this, "TODO: Share text when this is clicked", Toast.LENGTH_LONG).show();
    }

    /**
     * This is where you will create and fire off your own implicit Intent. Yours will be very
     * similar to what I've done above. You can view a list of implicit Intents on the Common
     * Intents page from the developer documentation.
     *
     * @see <http://developer.android.com/guide/components/intents-common.html/>
     *
     * @param v Button that was clicked.
     */
    public void createYourOwn(View v) {
        Toast.makeText(this,
                "TODO: Create Your Own Implicit Intent",
                Toast.LENGTH_SHORT)
                .show();
    }

    /**
     * This method fires off an implicit Intent to open a webpage.
     *
     * @param url Url of webpage to open. Should start with http:// or https:// as that is the
     *            scheme of the URI expected with this Intent according to the Common Intents page
     */
    private void openWebPage(String url) {
        /*
         * We wanted to demonstrate the Uri.parse method because its usage occurs frequently. You
         * could have just as easily passed in a Uri as the parameter of this method.
         */
        Uri webpage = Uri.parse(url);

        /*
         * Here, we create the Intent with the action of ACTION_VIEW. This action allows the user
         * to view particular content. In this case, our webpage URL.
         */
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage);

        /*
         * This is a check we perform with every implicit Intent that we launch. In some cases,
         * the device where this code is running might not have an Activity to perform the action
         * with the data we've specified. Without this check, in those cases your app would crash.
         */
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }


    // TODO (1) Create a method called showMap with a Uri as the single parameter
    // Do steps 2 - 4 within the showMap method
        // TODO (2) Create an Intent with action type, Intent.ACTION_VIEW

        // TODO (3) Set the data of the Intent to the Uri passed into this method

        // TODO (4) Verify that this Intent can be launched and then call startActivity


}
@@ -1785,361 +1785,268 @@
     
-// TODO (5) Store an address in a String%0A%0A        // TODO (6) Use Uri.parse with the appropriate scheme and query to form the Uri for the address%0A%0A        // TODO (7) Replace the Toast with a call to showMap, passing in the Uri from the previous step%0A        Toast.makeText(this, %22TODO: Open a map when this button is clicked%22, Toast.LENGTH_SHORT).show(
+String address = %221600 Amphitheatre Parkway, CA%22;%0A%0A        Uri.Builder builder = new Uri.Builder();%0A        builder.scheme(%22geo%22)%0A                .path(%220,0%22)%0A                .query(address);%0A        Uri addressUri = builder.build();%0A        showMap(addressUri
 );%0A 
@@ -4291,194 +4291,78 @@
  %7D%0A%0A
-%0A
     
-// TODO (1) Create a method called showMap with a Uri as the single parameter%0A    // Do steps 2 - 4 within the showMap method%0A        // TODO (2) Create an Intent with action type, 
+public void showMap(Uri uri) %7B%0A        Intent intent = new Intent(
 Inte
@@ -4375,17 +4375,18 @@
 ION_VIEW
-%0A
+);
 %0A       
@@ -4390,170 +4390,145 @@
     
-// TODO (3) Set the data of the Intent to the Uri passed into this method%0A%0A        // TODO (4) Verify that this Intent can be launched and then call startActivity%0A%0A
+intent.setData(uri);%0A%0A        if (intent.resolveActivity(getPackageManager()) != null) %7B%0A            startActivity(intent);%0A        %7D%0A    %7D
 %0A%7D

Solve open map