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