1/*
2 * Copyright (C) 2017 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
17package com.android.tv.license;
18
19import android.app.DialogFragment;
20import android.os.Bundle;
21import android.support.annotation.Nullable;
22import android.text.TextUtils;
23import android.text.method.ScrollingMovementMethod;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.view.ViewGroup.LayoutParams;
28import android.widget.TextView;
29
30import com.android.tv.R;
31import com.android.tv.dialog.SafeDismissDialogFragment;
32
33/** A DialogFragment that shows a License in a text view. */
34public class LicenseDialogFragment extends SafeDismissDialogFragment {
35    public static final String DIALOG_TAG = LicenseDialogFragment.class.getSimpleName();
36
37    private static final String LICENSE = "LICENSE";
38
39    private License mLicense;
40    private String mTrackerLabel;
41
42    /**
43     * Create a new LicenseDialogFragment to show a particular license.
44     *
45     * @param license The License to show.
46     */
47    public static LicenseDialogFragment newInstance(License license) {
48        LicenseDialogFragment f = new LicenseDialogFragment();
49        Bundle args = new Bundle();
50        args.putParcelable(LICENSE, license);
51        f.setArguments(args);
52        return f;
53    }
54
55    @Override
56    public void onCreate(Bundle savedInstanceState) {
57        super.onCreate(savedInstanceState);
58        mLicense = getArguments().getParcelable(LICENSE);
59        String title = mLicense.getLibraryName();
60        mTrackerLabel = getArguments().getString(title + "_license");
61        int style =
62                TextUtils.isEmpty(title)
63                        ? DialogFragment.STYLE_NO_TITLE
64                        : DialogFragment.STYLE_NORMAL;
65        setStyle(style, 0);
66    }
67
68    @Nullable
69    @Override
70    public View onCreateView(
71            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
72        TextView textView = new TextView(getActivity());
73        String licenseText = Licenses.getLicenseText(getContext(), mLicense);
74        textView.setText(licenseText != null ? licenseText : "");
75        textView.setMovementMethod(new ScrollingMovementMethod());
76        int verticalOverscan =
77                getResources().getDimensionPixelSize(R.dimen.vertical_overscan_safe_margin);
78        int horizontalOverscan =
79                getResources().getDimensionPixelSize(R.dimen.horizontal_overscan_safe_margin);
80        textView.setPadding(
81                horizontalOverscan, verticalOverscan, horizontalOverscan, verticalOverscan);
82        return textView;
83    }
84
85    @Override
86    public void onStart() {
87        super.onStart();
88        // Ensure the dialog is fullscreen, even if the TextView doesn't have its content yet.
89        getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
90        getDialog().setTitle(mLicense.getLibraryName());
91    }
92
93    @Override
94    public String getTrackerLabel() {
95        return mTrackerLabel;
96    }
97}
98