package com.example.omgandroid;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ShareActionProvider;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener,
        AdapterView.OnItemClickListener {

    TextView mainTextView;
    Button mainButton;
    EditText mainEditText;
    ListView mainListView;
    ArrayAdapter mArrayAdapter;
    ArrayList mNameList = new ArrayList();
    ShareActionProvider mShareActionProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1. Access the Textview defined in layout XML
        //and then set its text
        mainTextView = (TextView) findViewById(R.id.main_textview);
        mainTextView.setText("Set in JAVA code!");

        //2. Access the Button defined in layout XML
        // and listen for it here
        mainButton = (Button) findViewById(R.id.main_button);
        mainButton.setOnClickListener(this);

        //3. Access the EditText defined in layout XML
        mainEditText = (EditText) findViewById(R.id.main_edittext);

        //4. Access the ListView
        mainListView = (ListView) findViewById(R.id.main_listview);
        //4.1 Create the ArrayAdapter for the ListView
        mArrayAdapter = new ArrayAdapter(this,
                        android.R.layout.simple_list_item_1,
                        mNameList);
        //4.2 Set the ListView to use the ArrayAdapter
        mainListView.setAdapter(mArrayAdapter);

        //5. Set this Activity to react to list items being pressed
        mainListView.setOnItemClickListener(this);

        /*if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }*/
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        //Access the Share Item defined in menu XML
        MenuItem shareItem = menu.findItem(R.id.menu_item_share);

        //Access the object responsible for putting together the sharing sub-menu
        if(shareItem != null){
            mShareActionProvider = (ShareActionProvider) shareItem.getActionProvider();
        }

        //Create an Intent to share your content
        setShareIntent();
        return true;
    }

    private void setShareIntent(){
        if(mShareActionProvider != null){
            //create an Intent with the contents of the Textview
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Android Development");
            shareIntent.putExtra(Intent.EXTRA_TEXT, mainTextView.getText());

            //Make sure the provider knows it should work with that Intent
            mShareActionProvider.setShareIntent(shareIntent);
        }
    }

    /* THE TUTORIAL SAYS THAT WE SHOULD DELETE THIS IF ITS ALREADY HERE
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }*/

    @Override
    public void onClick(View view) {
        mainTextView.setText(mainEditText.getText().toString()
                + " is learning Android development from a tutorial!");

        //4.4 Also add that value to the list shown in the ListView
        mNameList.add(mainEditText.getText().toString());
        mArrayAdapter.notifyDataSetChanged();

        //6. The text you'd like to share has changed, and you need to update
        setShareIntent();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Log the item's position and contents to the console in Debug
        Log.d("omg android", position + ": " + mNameList.get(position));
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    /*public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }*/

}

@@ -141,24 +141,66 @@
 ent.Intent;%0A
+import android.content.SharedPreferences;%0A
 import andro
@@ -213,16 +213,16 @@
 Bundle;%0A
-
 import a
@@ -651,16 +651,45 @@
 extView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -1054,16 +1054,159 @@
 rovider;
+%0A    private static final String PREFS = %22prefs%22;%0A    private static final String PREF_NAME = %22name%22;%0A    SharedPreferences mSharedPreferences;
 %0A%0A    @O
@@ -2406,32 +2406,117 @@
 istener(this);%0A%0A
+        //7. Greet the user, or ask for their name if new%0A        displayWelcome();%0A%0A
         /*if (sa
@@ -2690,24 +2690,24 @@
  .commit();%0A
-
         %7D*/%0A
@@ -2713,16 +2713,589 @@
 %0A    %7D%0A%0A
+    private void displayWelcome() %7B%0A        //Access the device's key-value storage%0A        mSharedPreferences = getSharedPreferences(PREFS, MODE_PRIVATE);%0A%0A        String name = mSharedPreferences.getString(PREF_NAME, %22%22);%0A        //Read the user's name or an empty string if nothing found%0A        if (name.length() %3E 0) %7B%0A            //If the name is valid, displays a Toast welcoming them%0A            Toast.makeText(this,%22Welcome back %22 + name + %22!%22, Toast.LENGTH_LONG);%0A        %7Delse%7B%0A            Toast.makeText(this,%22First hi!! %22, Toast.LENGTH_LONG);%0A        %7D%0A    %7D%0A%0A
 %0A    @Ov

Create displayWelcome()