1 /* 2 * Copyright (C) 2015 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 package com.android.car.systemupdater; 17 18 import static com.android.car.systemupdater.UpdateLayoutFragment.EXTRA_RESUME_UPDATE; 19 20 import android.Manifest; 21 import android.content.pm.PackageManager; 22 import android.os.Bundle; 23 import android.view.MenuItem; 24 25 import androidx.appcompat.app.AppCompatActivity; 26 import androidx.appcompat.widget.Toolbar; 27 import androidx.core.app.ActivityCompat; 28 import androidx.core.content.ContextCompat; 29 30 import java.io.File; 31 32 /** 33 * Apply a system update using an ota package on internal or external storage. 34 */ 35 public class SystemUpdaterActivity extends AppCompatActivity 36 implements DeviceListFragment.SystemUpdater { 37 38 private static final String FRAGMENT_TAG = "FRAGMENT_TAG"; 39 private static final int STORAGE_PERMISSIONS_REQUEST_CODE = 0; 40 private static final String[] REQUIRED_STORAGE_PERMISSIONS = new String[]{ 41 Manifest.permission.READ_EXTERNAL_STORAGE, 42 Manifest.permission.WRITE_EXTERNAL_STORAGE, 43 Manifest.permission.WRITE_MEDIA_STORAGE 44 }; 45 46 @Override onCreate(Bundle savedInstanceState)47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 50 if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) 51 != PackageManager.PERMISSION_GRANTED) { 52 ActivityCompat.requestPermissions(this, REQUIRED_STORAGE_PERMISSIONS, 53 STORAGE_PERMISSIONS_REQUEST_CODE); 54 } 55 56 setContentView(R.layout.activity_main); 57 Toolbar toolbar = findViewById(R.id.toolbar); 58 setSupportActionBar(toolbar); 59 60 if (savedInstanceState == null) { 61 Bundle intentExtras = getIntent().getExtras(); 62 if (intentExtras != null && intentExtras.getBoolean(EXTRA_RESUME_UPDATE)) { 63 UpdateLayoutFragment fragment = UpdateLayoutFragment.newResumedInstance(); 64 getSupportFragmentManager().beginTransaction() 65 .replace(R.id.device_container, fragment, FRAGMENT_TAG) 66 .commitNow(); 67 } else { 68 DeviceListFragment fragment = new DeviceListFragment(); 69 getSupportFragmentManager().beginTransaction() 70 .replace(R.id.device_container, fragment, FRAGMENT_TAG) 71 .commitNow(); 72 } 73 } 74 } 75 76 @Override onOptionsItemSelected(MenuItem item)77 public boolean onOptionsItemSelected(MenuItem item) { 78 if (item.getItemId() == android.R.id.home) { 79 UpFragment upFragment = 80 (UpFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG); 81 if (!upFragment.goUp()) { 82 onBackPressed(); 83 } 84 return true; 85 } 86 return super.onOptionsItemSelected(item); 87 } 88 89 @Override onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)90 public void onRequestPermissionsResult(int requestCode, String permissions[], 91 int[] grantResults) { 92 super.onRequestPermissionsResult(requestCode, permissions, grantResults); 93 if (STORAGE_PERMISSIONS_REQUEST_CODE == requestCode) { 94 if (grantResults.length == 0) { 95 finish(); 96 } 97 for (int grantResult : grantResults) { 98 if (grantResult != PackageManager.PERMISSION_GRANTED) { 99 finish(); 100 } 101 } 102 } 103 } 104 105 @Override applyUpdate(File file)106 public void applyUpdate(File file) { 107 UpdateLayoutFragment fragment = UpdateLayoutFragment.getInstance(file); 108 getSupportFragmentManager().beginTransaction() 109 .replace(R.id.device_container, fragment, FRAGMENT_TAG) 110 .addToBackStack(null) 111 .commit(); 112 } 113 } 114