1/*******************************************************************************
2 *      Copyright (C) 2014 Google Inc.
3 *      Licensed to The Android Open Source Project.
4 *
5 *      Licensed under the Apache License, Version 2.0 (the "License");
6 *      you may not use this file except in compliance with the License.
7 *      You may obtain a copy of the License at
8 *
9 *           http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *      Unless required by applicable law or agreed to in writing, software
12 *      distributed under the License is distributed on an "AS IS" BASIS,
13 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *      See the License for the specific language governing permissions and
15 *      limitations under the License.
16 *******************************************************************************/
17
18package com.android.mail.ui.settings;
19
20import android.app.Activity;
21import android.content.Intent;
22import android.net.Uri;
23import android.os.Bundle;
24import android.preference.PreferenceActivity;
25import android.text.TextUtils;
26
27import java.util.Set;
28
29/**
30 * Activity that allows directly launching into the preference activity, from external parties
31 */
32public class PublicPreferenceActivity extends Activity {
33
34    // TODO: Temporary. Once the app-specific preference activities are deleted, this will no longer
35    // be needed. This is set by the application subclasses
36    public static Class<? extends MailPreferenceActivity> sPreferenceActivityClass;
37
38    @Override
39    public void onCreate(Bundle savedInstanceState) {
40        super.onCreate(savedInstanceState);
41
42        final Intent intent = getIntent();
43
44        // We need to remove the extra that allows a fragment to be directly opened
45        intent.removeExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT);
46        intent.removeExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
47        intent.removeExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_SHORT_TITLE);
48        intent.removeExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_TITLE);
49
50        // Remove any fragment specifier from the data uri
51        final Uri dataUri = intent.getData();
52        if (dataUri != null) {
53            final String fragmentIdStr =
54                    dataUri.getQueryParameter(MailPreferenceActivity.PREFERENCE_FRAGMENT_ID);
55            if (fragmentIdStr != null) {
56                final Set<String> paramNames = dataUri.getQueryParameterNames();
57
58                final Uri.Builder builder = dataUri.buildUpon().clearQuery();
59
60                for (String param : paramNames) {
61                    if (!TextUtils.equals(param, MailPreferenceActivity.PREFERENCE_FRAGMENT_ID)) {
62                        builder.appendQueryParameter(param, dataUri.getQueryParameter(param));
63                    }
64                }
65                intent.setData(builder.build());
66            }
67        }
68
69        if (sPreferenceActivityClass == null) {
70            sPreferenceActivityClass = MailPreferenceActivity.class;
71        }
72        // Force this intent to be sent to the appropriate MailPreferenceActivity class
73        intent.setClass(this, sPreferenceActivityClass);
74
75        startActivity(intent);
76        finish();
77    }
78}
79