package com.yininghuang.zhihudailynews.settings;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;

import com.yininghuang.zhihudailynews.Constants;
import com.yininghuang.zhihudailynews.R;
import com.yininghuang.zhihudailynews.utils.ActivityUtils;

import java.io.File;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Yining Huang on 2016/10/22.
 */

public class AboutFragment extends PreferenceFragmentCompat {



    public static AboutFragment newInstance() {
        return new AboutFragment();
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.fragment_about);

        findPreference("license").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                ActivityUtils.addFragment(
                        getActivity().getSupportFragmentManager(),
                        LicenseFragment.newInstance(),
                        R.id.mainFrameLayout,
                        "LicenseFragment");
                return true;
            }
        });

        findPreference("repo_url").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Uri uri = Uri.parse(Constants.GITHUB_REPO_URL);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                return true;
            }
        });

        findPreference("zhihu_url").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Uri uri = Uri.parse(Constants.ZHIHU_USER_HOME_URL);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                return true;
            }
        });

        findPreference("mail").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                intent.setType("plain/text");
                intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{Constants.E_MAIL});
                startActivity(intent);
                return false;
            }
        });

        try {
            PackageManager pm = getActivity().getPackageManager();
            String name = pm.getPackageInfo(getActivity().getPackageName(), 0).versionName;
            findPreference("version").setSummary(name);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getActivity().setTitle(R.string.about);
    }
}

@@ -39,24 +39,74 @@
 .settings;%0A%0A
+import android.content.ActivityNotFoundException;%0A
 import andro
@@ -124,16 +124,16 @@
 Intent;%0A
-
 import a
@@ -220,55 +220,8 @@
 le;%0A
-import android.support.design.widget.Snackbar;%0A
 impo
@@ -483,209 +483,8 @@
 s;%0A%0A
-import java.io.File;%0A%0Aimport rx.Observable;%0Aimport rx.Subscriber;%0Aimport rx.Subscription;%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.functions.Action1;%0Aimport rx.schedulers.Schedulers;%0A%0A
 /**%0A
@@ -594,17 +594,16 @@
 pat %7B%0A%0A%0A
-%0A
     publ
@@ -1676,32 +1676,58 @@
 ION_VIEW, uri);%0A
+                try %7B%0A    
                 
@@ -1741,32 +1741,147 @@
 tivity(intent);%0A
+                %7D catch (ActivityNotFoundException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A
                 
@@ -2251,32 +2251,58 @@
 ION_VIEW, uri);%0A
+                try %7B%0A    
                 
@@ -2316,32 +2316,147 @@
 tivity(intent);%0A
+                %7D catch (ActivityNotFoundException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A
                 
@@ -2919,16 +2919,42 @@
 MAIL%7D);%0A
+                try %7B%0A    
         
@@ -2976,32 +2976,147 @@
 tivity(intent);%0A
+                %7D catch (ActivityNotFoundException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A
                 

fix ActivityNotFoundException