blob: ee1c1c8f2283cd765b5ebd5bf364f6aa1567f42a [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;
26import androidx.appcompat.widget.Toolbar;
27import androidx.core.app.ActivityCompat;
28import androidx.core.content.ContextCompat;
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070029
30import java.io.File;
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070031
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070032/**
Nicholas Sauer59eeada2018-01-31 16:40:37 -080033 * Apply a system update using an ota package on internal or external storage.
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070034 */
Nicholas Sauerf4498752018-02-02 23:52:29 -080035public class SystemUpdaterActivity extends AppCompatActivity
36 implements DeviceListFragment.SystemUpdater {
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070037
Nicholas Sauer80fcb282018-05-14 20:28:06 -070038 private static final String FRAGMENT_TAG = "FRAGMENT_TAG";
Nicholas Sauer2dc00462018-02-09 21:58:42 -080039 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,
Xihua Chen63c2fc62018-10-25 15:32:32 +080042 Manifest.permission.WRITE_EXTERNAL_STORAGE,
43 Manifest.permission.WRITE_MEDIA_STORAGE
Nicholas Sauer2dc00462018-02-09 21:58:42 -080044 };
45
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070046 @Override
47 protected void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
Nicholas Sauer2dc00462018-02-09 21:58:42 -080049
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
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070056 setContentView(R.layout.activity_main);
Nicholas Sauerf4498752018-02-02 23:52:29 -080057 Toolbar toolbar = findViewById(R.id.toolbar);
Nicholas Sauer59eeada2018-01-31 16:40:37 -080058 setSupportActionBar(toolbar);
59
Nicholas Sauerf4498752018-02-02 23:52:29 -080060 if (savedInstanceState == null) {
Nicholas Sauer61a349c2018-04-17 13:11:45 -070061 Bundle intentExtras = getIntent().getExtras();
62 if (intentExtras != null && intentExtras.getBoolean(EXTRA_RESUME_UPDATE)) {
63 UpdateLayoutFragment fragment = UpdateLayoutFragment.newResumedInstance();
64 getSupportFragmentManager().beginTransaction()
Nicholas Sauer80fcb282018-05-14 20:28:06 -070065 .replace(R.id.device_container, fragment, FRAGMENT_TAG)
Nicholas Sauer61a349c2018-04-17 13:11:45 -070066 .commitNow();
67 } else {
68 DeviceListFragment fragment = new DeviceListFragment();
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 }
Rakesh Iyer3506f3f2016-10-19 23:58:48 -070073 }
74 }
75
76 @Override
Nicholas Sauer80fcb282018-05-14 20:28:06 -070077 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
Nicholas Sauer2dc00462018-02-09 21:58:42 -080090 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
Nicholas Sauer59eeada2018-01-31 16:40:37 -0800106 public void applyUpdate(File file) {
Nicholas Sauerf4498752018-02-02 23:52:29 -0800107 UpdateLayoutFragment fragment = UpdateLayoutFragment.getInstance(file);
108 getSupportFragmentManager().beginTransaction()
Nicholas Sauer80fcb282018-05-14 20:28:06 -0700109 .replace(R.id.device_container, fragment, FRAGMENT_TAG)
Nicholas Sauerf4498752018-02-02 23:52:29 -0800110 .addToBackStack(null)
111 .commit();
Rakesh Iyer3506f3f2016-10-19 23:58:48 -0700112 }
Rakesh Iyer3506f3f2016-10-19 23:58:48 -0700113}