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.dialer.about;
18
19import android.os.Bundle;
20import android.support.v7.app.AppCompatActivity;
21import android.view.MenuItem;
22import android.widget.ScrollView;
23import android.widget.TextView;
24
25/** Simple Activity that renders locally stored open source legal info in a text view. */
26public final class LicenseActivity extends AppCompatActivity {
27  private static final String TAG = "LicenseActivity";
28  private static final String STATE_SCROLL_POS = "scroll_pos";
29
30  @Override
31  public void onCreate(Bundle bundle) {
32    super.onCreate(bundle);
33    setContentView(R.layout.license_scrollview);
34
35    License license = getIntent().getParcelableExtra(LicenseMenuActivity.ARGS_LICENSE);
36    getSupportActionBar().setTitle(license.getLibraryName());
37
38    // Show 'up' button with no logo.
39    getSupportActionBar().setDisplayShowHomeEnabled(true);
40    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
41    getSupportActionBar().setLogo(null);
42
43    TextView textView = (TextView) findViewById(R.id.license_activity_textview);
44    String licenseText = Licenses.getLicenseText(this, license);
45    if (licenseText == null) {
46      finish();
47      return;
48    }
49    textView.setText(licenseText);
50  }
51
52  @Override
53  public void onSaveInstanceState(Bundle outState) {
54    super.onSaveInstanceState(outState);
55    ScrollView scrollView = (ScrollView) findViewById(R.id.license_activity_scrollview);
56    TextView textView = (TextView) findViewById(R.id.license_activity_textview);
57    int firstVisibleLine = textView.getLayout().getLineForVertical(scrollView.getScrollY());
58    int firstVisibleChar = textView.getLayout().getLineStart(firstVisibleLine);
59    outState.putInt(STATE_SCROLL_POS, firstVisibleChar);
60  }
61
62  @Override
63  public void onRestoreInstanceState(Bundle savedInstanceState) {
64    super.onRestoreInstanceState(savedInstanceState);
65    final ScrollView scrollView = (ScrollView) findViewById(R.id.license_activity_scrollview);
66    final int firstVisibleChar = savedInstanceState.getInt(STATE_SCROLL_POS);
67    scrollView.post(
68        new Runnable() {
69          @Override
70          public void run() {
71            TextView textView = (TextView) findViewById(R.id.license_activity_textview);
72            int firstVisibleLine = textView.getLayout().getLineForOffset(firstVisibleChar);
73            int offset = textView.getLayout().getLineTop(firstVisibleLine);
74            scrollView.scrollTo(0, offset);
75          }
76        });
77  }
78
79  @Override
80  public boolean onOptionsItemSelected(final MenuItem item) {
81    if (item.getItemId() == android.R.id.home) {
82      finish();
83      return true;
84    }
85    return super.onOptionsItemSelected(item);
86  }
87}
88