1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.contacts.preference;
18 
19 import android.content.ActivityNotFoundException;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageInfo;
23 import android.content.pm.PackageManager;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.preference.Preference;
27 import android.preference.PreferenceFragment;
28 import android.widget.Toast;
29 
30 import com.android.contacts.R;
31 import com.android.contacts.activities.LicenseActivity;
32 
33 /**
34  * This fragment shows the preferences for "about".
35  */
36 public class AboutPreferenceFragment extends PreferenceFragment {
37 
38     public static final String PRIVACY_POLICY_URL = "http://www.google.com/policies/privacy";
39     public static final String TERMS_OF_SERVICE_URL = "http://www.google.com/policies/terms";
40 
newInstance()41     public static AboutPreferenceFragment newInstance() {
42         return new AboutPreferenceFragment();
43     }
44 
45     @Override
onCreate(Bundle savedInstanceState)46     public void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48 
49         // Load the preferences from an XML resource
50         addPreferencesFromResource(R.xml.preference_about);
51 
52         // Set build version of Contacts App.
53         final PackageManager manager = getActivity().getPackageManager();
54         try {
55             final PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
56             final Preference versionPreference = findPreference(
57                     getString(R.string.pref_build_version_key));
58             versionPreference.setSummary(info.versionName);
59         } catch (PackageManager.NameNotFoundException e) {
60             // Nothing
61         }
62 
63         final Preference licensePreference = findPreference(
64                 getString(R.string.pref_open_source_licenses_key));
65         licensePreference.setIntent(new Intent(getActivity(), LicenseActivity.class));
66 
67         final Preference privacyPolicyPreference = findPreference("pref_privacy_policy");
68         final Preference termsOfServicePreference = findPreference("pref_terms_of_service");
69 
70         final Preference.OnPreferenceClickListener listener =
71                 new Preference.OnPreferenceClickListener() {
72             @Override
73             public boolean onPreferenceClick(Preference preference) {
74                 try {
75                     if (preference == privacyPolicyPreference) {
76                         startActivityForUrl(PRIVACY_POLICY_URL);
77                     } else if (preference == termsOfServicePreference) {
78                         startActivityForUrl(TERMS_OF_SERVICE_URL);
79                     }
80                 } catch (ActivityNotFoundException ex) {
81                     Toast.makeText(getContext(), getString(R.string.url_open_error_toast),
82                             Toast.LENGTH_SHORT).show();
83                 }
84                 return true;
85             }
86         };
87 
88         privacyPolicyPreference.setOnPreferenceClickListener(listener);
89         termsOfServicePreference.setOnPreferenceClickListener(listener);
90     }
91 
92     @Override
getContext()93     public Context getContext() {
94         return getActivity();
95     }
96 
startActivityForUrl(String urlString)97     private void startActivityForUrl(String urlString) {
98         final Intent intent = new Intent();
99         intent.setAction(Intent.ACTION_VIEW);
100         intent.setData(Uri.parse(urlString));
101         startActivity(intent);
102     }
103 }
104 
105