Home | History | Annotate | Download | only in preference
      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.common.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.common.R;
     31 import com.android.contacts.common.activity.LicenseActivity;
     32 
     33 /**
     34  * This fragment shows the preferences for "about".
     35  */
     36 public class AboutPreferenceFragment extends PreferenceFragment {
     37 
     38     private static final String PRIVACY_POLICY_URL = "http://www.google.com/policies/privacy";
     39     private static final String TERMS_OF_SERVICE_URL = "http://www.google.com/policies/terms";
     40 
     41     @Override
     42     public void onCreate(Bundle savedInstanceState) {
     43         super.onCreate(savedInstanceState);
     44 
     45         // Load the preferences from an XML resource
     46         addPreferencesFromResource(R.xml.preference_about);
     47 
     48         // Set build version of Contacts App.
     49         final PackageManager manager = getActivity().getPackageManager();
     50         try {
     51             final PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
     52             final Preference versionPreference = findPreference(
     53                     getString(R.string.pref_build_version_key));
     54             versionPreference.setSummary(info.versionName);
     55         } catch (PackageManager.NameNotFoundException e) {
     56             // Nothing
     57         }
     58 
     59         final Preference licensePreference = findPreference(
     60                 getString(R.string.pref_open_source_licenses_key));
     61         licensePreference.setIntent(new Intent(getActivity(), LicenseActivity.class));
     62 
     63         final Preference privacyPolicyPreference = findPreference("pref_privacy_policy");
     64         final Preference termsOfServicePreference = findPreference("pref_terms_of_service");
     65 
     66         final Preference.OnPreferenceClickListener listener =
     67                 new Preference.OnPreferenceClickListener() {
     68             @Override
     69             public boolean onPreferenceClick(Preference preference) {
     70                 try {
     71                     if (preference == privacyPolicyPreference) {
     72                         startActivityForUrl(PRIVACY_POLICY_URL);
     73                     } else if (preference == termsOfServicePreference) {
     74                         startActivityForUrl(TERMS_OF_SERVICE_URL);
     75                     }
     76                 } catch (ActivityNotFoundException ex) {
     77                     Toast.makeText(getContext(), getString(R.string.url_open_error_toast),
     78                             Toast.LENGTH_SHORT).show();
     79                 }
     80                 return true;
     81             }
     82         };
     83 
     84         privacyPolicyPreference.setOnPreferenceClickListener(listener);
     85         termsOfServicePreference.setOnPreferenceClickListener(listener);
     86     }
     87 
     88     @Override
     89     public Context getContext() {
     90         return getActivity();
     91     }
     92 
     93     private void startActivityForUrl(String urlString) {
     94         final Intent intent = new Intent();
     95         intent.setAction(Intent.ACTION_VIEW);
     96         intent.setData(Uri.parse(urlString));
     97         startActivity(intent);
     98     }
     99 }
    100 
    101