blob: 25b8cf4f9b133dd4c34a60b8a29d89fcc97fd6c5 [file] [log] [blame]
Rakesh Iyer3506f3f2016-10-19 23:58:48 -07001/*
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 */
16package com.android.car.systemupdater;
17
Nicholas Sauer61a349c2018-04-17 13:11:45 -070018import static com.android.car.systemupdater.UpdateLayoutFragment.EXTRA_RESUME_UPDATE;
19
Nicholas Sauer2dc00462018-02-09 21:58:42 -080020import android.Manifest;
21import android.content.pm.PackageManager;
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070022import android.os.Bundle;
Nicholas Sauer80fcb282018-05-14 20:28:06 -070023import android.view.MenuItem;
Anthony Chenb18fd192018-04-13 14:20:52 -070024
25import androidx.appcompat.app.AppCompatActivity;
Anthony Chenb18fd192018-04-13 14:20:52 -070026import androidx.core.app.ActivityCompat;
27import androidx.core.content.ContextCompat;
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070028
Ram Parameswaraneb6d7f22020-03-25 19:11:27 -070029import com.android.car.ui.core.CarUi;
30import com.android.car.ui.toolbar.Toolbar;
31import com.android.car.ui.toolbar.ToolbarController;
32
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070033import java.io.File;
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070034
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070035/**
Nicholas Sauer59eeada2018-01-31 16:40:37 -080036 * Apply a system update using an ota package on internal or external storage.
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070037 */
Nicholas Sauerf4498752018-02-02 23:52:29 -080038public class SystemUpdaterActivity extends AppCompatActivity
39 implements DeviceListFragment.SystemUpdater {
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070040
Nicholas Sauer80fcb282018-05-14 20:28:06 -070041 private static final String FRAGMENT_TAG = "FRAGMENT_TAG";
Nicholas Sauer2dc00462018-02-09 21:58:42 -080042 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,
Xihua Chen63c2fc62018-10-25 15:32:32 +080045 Manifest.permission.WRITE_EXTERNAL_STORAGE,
46 Manifest.permission.WRITE_MEDIA_STORAGE
Nicholas Sauer2dc00462018-02-09 21:58:42 -080047 };
48
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070049 @Override
50 protected void onCreate(Bundle savedInstanceState) {
51 super.onCreate(savedInstanceState);
Nicholas Sauer2dc00462018-02-09 21:58:42 -080052
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
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070059 setContentView(R.layout.activity_main);
Ram Parameswaraneb6d7f22020-03-25 19:11:27 -070060
61 ToolbarController toolbar = CarUi.requireToolbar(this);
62 toolbar.setTitle(getString(R.string.title));
63 toolbar.setState(Toolbar.State.SUBPAGE);
Nicholas Sauer59eeada2018-01-31 16:40:37 -080064
Nicholas Sauerf4498752018-02-02 23:52:29 -080065 if (savedInstanceState == null) {
Nicholas Sauer61a349c2018-04-17 13:11:45 -070066 Bundle intentExtras = getIntent().getExtras();
67 if (intentExtras != null && intentExtras.getBoolean(EXTRA_RESUME_UPDATE)) {
68 UpdateLayoutFragment fragment = UpdateLayoutFragment.newResumedInstance();
69 getSupportFragmentManager().beginTransaction()
Nicholas Sauer80fcb282018-05-14 20:28:06 -070070 .replace(R.id.device_container, fragment, FRAGMENT_TAG)
Nicholas Sauer61a349c2018-04-17 13:11:45 -070071 .commitNow();
72 } else {
73 DeviceListFragment fragment = new DeviceListFragment();
74 getSupportFragmentManager().beginTransaction()
Nicholas Sauer80fcb282018-05-14 20:28:06 -070075 .replace(R.id.device_container, fragment, FRAGMENT_TAG)
Nicholas Sauer61a349c2018-04-17 13:11:45 -070076 .commitNow();
77 }
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070078 }
79 }
80
81 @Override
Nicholas Sauer80fcb282018-05-14 20:28:06 -070082 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
Nicholas Sauer2dc00462018-02-09 21:58:42 -080095 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
Nicholas Sauer59eeada2018-01-31 16:40:37 -0800111 public void applyUpdate(File file) {
Nicholas Sauerf4498752018-02-02 23:52:29 -0800112 UpdateLayoutFragment fragment = UpdateLayoutFragment.getInstance(file);
113 getSupportFragmentManager().beginTransaction()
Nicholas Sauer80fcb282018-05-14 20:28:06 -0700114 .replace(R.id.device_container, fragment, FRAGMENT_TAG)
Nicholas Sauerf4498752018-02-02 23:52:29 -0800115 .addToBackStack(null)
116 .commit();
Rakesh Iyer3506f3f2016-10-19 23:58:48 -0700117 }
Rakesh Iyer3506f3f2016-10-19 23:58:48 -0700118}