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