/*
Copyright (c) 2014-2015 F-Secure
See LICENSE for details
*/
package cc.softwarefactory.lokki.android;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.androidquery.AQuery;
import cc.softwarefactory.lokki.android.utils.Utils;


public class AboutFragment extends Fragment {

    private static final String TAG = "About";
    private AQuery aq;
    private String[] aboutLinksUrls;

    public AboutFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.activity_about, container, false);
        aq = new AQuery(getActivity(), rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        aboutLinksUrls = getResources().getStringArray(R.array.about_links_url);
        String[] aboutLinks = getResources().getStringArray(R.array.about_links);
        aq.id(R.id.listView1).adapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, aboutLinks));
        aq.id(R.id.listView1).itemClicked(this, "onItemSelected");
        try {
            aq.id(R.id.version).text("Version: " + Utils.getAppVersion(getActivity()) + getResources().getString(R.string.version_and_copyright));
        } catch (Exception ex) {
        }
    }

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        Log.e(TAG, "onItemSelected: " + position + ", tag:" + parent.getTag());
        String url = aboutLinksUrls[position];

        switch (position) {

            case 0: // Help
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                break;

            case 1: // Send feedback
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                break;

            case 2: // Tell a friend about Lokki
                try {
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject));
                    intent.putExtra(Intent.EXTRA_TEXT, getResources().getString(R.string.share_text));
                    startActivity(Intent.createChooser(intent, getResources().getString(R.string.share)));

                } catch (ActivityNotFoundException anfe) {
                }
                break;

        }
    }
}

@@ -176,24 +176,66 @@
 ent.Intent;%0A
+import android.content.pm.PackageManager;%0A
 import andro
@@ -680,16 +680,24 @@
 = %22About
+Fragment
 %22;%0A    p
@@ -1544,33 +1544,40 @@
 ked(
-this, %22onItemSelected%22
+new AboutItemClickListener()
 );%0A
+%0A
     
@@ -1750,16 +1750,43 @@
  catch (
+PackageManager.NameNotFound
 Exceptio
@@ -1788,17 +1788,16 @@
 eption e
-x
 ) %7B%0A    
@@ -1804,17 +1804,187 @@
     
-%7D%0A    %7D%0A%0A
+    Log.e(TAG, %22Couldn't set text in about screen%22);%0A        %7D%0A    %7D%0A%0A    private class AboutItemClickListener implements AdapterView.OnItemClickListener %7B%0A%0A        @Override%0A    
     
@@ -2001,24 +2001,21 @@
 d onItem
-Selected
+Click
 (Adapter
@@ -2062,25 +2062,28 @@
  long id) %7B%0A
-%0A
+    
         Log.
@@ -2154,24 +2154,28 @@
 ));%0A        
+    
 String url =
@@ -2202,16 +2202,20 @@
 tion%5D;%0A%0A
+    
         
@@ -2243,24 +2243,28 @@
             
+    
 case 0: // H
@@ -2267,16 +2267,20 @@
 // Help%0A
+    
         
@@ -2342,32 +2342,36 @@
 i.parse(url)));%0A
+    
                 
@@ -2386,24 +2386,28 @@
             
+    
 case 1: // S
@@ -2419,16 +2419,20 @@
 eedback%0A
+    
         
@@ -2494,32 +2494,36 @@
 i.parse(url)));%0A
+    
                 
@@ -2538,24 +2538,28 @@
             
+    
 case 2: // T
@@ -2591,38 +2591,46 @@
                 
+   
+ 
 try %7B%0A
+    
                 
@@ -2697,24 +2697,28 @@
             
+ 
+   
 intent.setTy
@@ -2731,24 +2731,28 @@
 xt/plain%22);%0A
+    
             
@@ -2860,32 +2860,36 @@
                 
+    
 intent.putExtra(
@@ -2967,32 +2967,36 @@
                 
+ 
+   
 startActivity(In
@@ -3075,32 +3075,36 @@
                 
+    
 %7D catch (Activit
@@ -3126,16 +3126,107 @@
 ion 
-anf
 e) %7B%0A
+                        Log.e(TAG, %22Couldn't open 'tell a friend about lokki' activity%22);%0A    
     
@@ -3247,39 +3247,56 @@
                 
+    
 break;%0A
+            %7D
 %0A        %7D%0A    %7D

Use a callback class instead of string