content: /*
   * Copyright (C) 2015 Bilibili
   * Copyright (C) 2015 Zhang Rui <bbcallen@gmail.com>
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.alanwang4523.a4ijkplayerdemo.application;
  
  import android.Manifest;
  import android.annotation.SuppressLint;
  import android.content.pm.PackageManager;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  
  import com.alanwang4523.a4ijkplayerdemo.R;
  import com.alanwang4523.a4ijkplayerdemo.activities.RecentMediaActivity;
  import com.alanwang4523.a4ijkplayerdemo.activities.SampleMediaActivity;
  import com.alanwang4523.a4ijkplayerdemo.activities.SettingsActivity;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.appcompat.widget.Toolbar;
  import androidx.core.app.ActivityCompat;
  import androidx.core.content.ContextCompat;
  
  @SuppressLint("Registered")
  public class AppActivity extends AppCompatActivity {
      private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_app);
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          if (ContextCompat.checkSelfPermission(this,
                  Manifest.permission.READ_EXTERNAL_STORAGE)
                  != PackageManager.PERMISSION_GRANTED) {
              if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                      Manifest.permission.READ_EXTERNAL_STORAGE)) {
                  // TODO: show explanation
              } else {
                  ActivityCompat.requestPermissions(this,
                          new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                          MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
              }
          }
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          switch (requestCode) {
              case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
                  if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                      // permission was granted, yay! Do the task you need to do.
                  } else {
                      // permission denied, boo! Disable the functionality that depends on this permission.
                  }
              }
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.menu_app, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              SettingsActivity.intentTo(this);
              return true;
          } else if (id == R.id.action_recent) {
              RecentMediaActivity.intentTo(this);
          } else if (id == R.id.action_sample) {
              SampleMediaActivity.intentTo(this);
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public boolean onPrepareOptionsMenu(Menu menu) {
          boolean show = super.onPrepareOptionsMenu(menu);
          if (!show)
              return show;
  
          return true;
      }
  }
  