1633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko/*
2633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko * Copyright (C) 2017 The Android Open Source Project
3633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko *
4633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko * Licensed under the Apache License, Version 2.0 (the "License");
5633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko * you may not use this file except in compliance with the License.
6633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko * You may obtain a copy of the License at
7633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko *
8633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko *      http://www.apache.org/licenses/LICENSE-2.0
9633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko *
10633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko * Unless required by applicable law or agreed to in writing, software
11633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko * distributed under the License is distributed on an "AS IS" BASIS,
12633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko * See the License for the specific language governing permissions and
14633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko * limitations under the License
15633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko */
16633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
17633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkopackage com.android.tv.license;
18633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
19633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.app.DialogFragment;
20633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.os.Bundle;
21633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.support.annotation.Nullable;
22633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.text.TextUtils;
23633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.text.method.ScrollingMovementMethod;
24633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.view.LayoutInflater;
25633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.view.View;
26633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.view.ViewGroup;
27633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.view.ViewGroup.LayoutParams;
28633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport android.widget.TextView;
29633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport com.android.tv.R;
30633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkoimport com.android.tv.dialog.SafeDismissDialogFragment;
31633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
32633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko/** A DialogFragment that shows a License in a text view. */
33633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalkopublic class LicenseDialogFragment extends SafeDismissDialogFragment {
34633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    public static final String DIALOG_TAG = LicenseDialogFragment.class.getSimpleName();
35633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
36633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private static final String LICENSE = "LICENSE";
37633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
38633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private License mLicense;
39633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    private String mTrackerLabel;
40633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
41633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    /**
42633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko     * Create a new LicenseDialogFragment to show a particular license.
43633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko     *
44633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko     * @param license The License to show.
45633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko     */
46633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    public static LicenseDialogFragment newInstance(License license) {
47633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        LicenseDialogFragment f = new LicenseDialogFragment();
48633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        Bundle args = new Bundle();
49633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        args.putParcelable(LICENSE, license);
50633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        f.setArguments(args);
51633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        return f;
52633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
53633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
54633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    @Override
55633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    public void onCreate(Bundle savedInstanceState) {
56633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        super.onCreate(savedInstanceState);
57633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        mLicense = getArguments().getParcelable(LICENSE);
58633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        String title = mLicense.getLibraryName();
59633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        mTrackerLabel = getArguments().getString(title + "_license");
60633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        int style =
61633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                TextUtils.isEmpty(title)
62633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                        ? DialogFragment.STYLE_NO_TITLE
63633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                        : DialogFragment.STYLE_NORMAL;
64633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        setStyle(style, 0);
65633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
66633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
67633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    @Nullable
68633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    @Override
69633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    public View onCreateView(
70633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
71633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        TextView textView = new TextView(getActivity());
72633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        String licenseText = Licenses.getLicenseText(getContext(), mLicense);
73633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        textView.setText(licenseText != null ? licenseText : "");
74633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        textView.setMovementMethod(new ScrollingMovementMethod());
75633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        int verticalOverscan =
76633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                getResources().getDimensionPixelSize(R.dimen.vertical_overscan_safe_margin);
77633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        int horizontalOverscan =
78633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                getResources().getDimensionPixelSize(R.dimen.horizontal_overscan_safe_margin);
79633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        textView.setPadding(
80633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko                horizontalOverscan, verticalOverscan, horizontalOverscan, verticalOverscan);
81633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        return textView;
82633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
83633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
84633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    @Override
85633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    public void onStart() {
86633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        super.onStart();
87633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        // Ensure the dialog is fullscreen, even if the TextView doesn't have its content yet.
88633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
89633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        getDialog().setTitle(mLicense.getLibraryName());
90633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
91633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko
92633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    @Override
93633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    public String getTrackerLabel() {
94633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko        return mTrackerLabel;
95633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko    }
96633eb826b8c97731dfc5ef12c7bf78a63734275dNick Chalko}
97