Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 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 | |
Nicholas Sauer | 61a349c | 2018-04-17 13:11:45 -0700 | [diff] [blame] | 18 | import static com.android.car.systemupdater.UpdateLayoutFragment.EXTRA_RESUME_UPDATE; |
| 19 | |
Nicholas Sauer | 2dc0046 | 2018-02-09 21:58:42 -0800 | [diff] [blame] | 20 | import android.Manifest; |
| 21 | import android.content.pm.PackageManager; |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 22 | import android.os.Bundle; |
Nicholas Sauer | 80fcb28 | 2018-05-14 20:28:06 -0700 | [diff] [blame] | 23 | import android.view.MenuItem; |
Anthony Chen | b18fd19 | 2018-04-13 14:20:52 -0700 | [diff] [blame] | 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; |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 29 | |
| 30 | import java.io.File; |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 31 | |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 32 | /** |
Nicholas Sauer | 59eeada | 2018-01-31 16:40:37 -0800 | [diff] [blame] | 33 | * Apply a system update using an ota package on internal or external storage. |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 34 | */ |
Nicholas Sauer | f449875 | 2018-02-02 23:52:29 -0800 | [diff] [blame] | 35 | public class SystemUpdaterActivity extends AppCompatActivity |
| 36 | implements DeviceListFragment.SystemUpdater { |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 37 | |
Nicholas Sauer | 80fcb28 | 2018-05-14 20:28:06 -0700 | [diff] [blame] | 38 | private static final String FRAGMENT_TAG = "FRAGMENT_TAG"; |
Nicholas Sauer | 2dc0046 | 2018-02-09 21:58:42 -0800 | [diff] [blame] | 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, |
Xihua Chen | 63c2fc6 | 2018-10-25 15:32:32 +0800 | [diff] [blame^] | 42 | Manifest.permission.WRITE_EXTERNAL_STORAGE, |
| 43 | Manifest.permission.WRITE_MEDIA_STORAGE |
Nicholas Sauer | 2dc0046 | 2018-02-09 21:58:42 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 46 | @Override |
| 47 | protected void onCreate(Bundle savedInstanceState) { |
| 48 | super.onCreate(savedInstanceState); |
Nicholas Sauer | 2dc0046 | 2018-02-09 21:58:42 -0800 | [diff] [blame] | 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 | |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 56 | setContentView(R.layout.activity_main); |
Nicholas Sauer | f449875 | 2018-02-02 23:52:29 -0800 | [diff] [blame] | 57 | Toolbar toolbar = findViewById(R.id.toolbar); |
Nicholas Sauer | 59eeada | 2018-01-31 16:40:37 -0800 | [diff] [blame] | 58 | setSupportActionBar(toolbar); |
| 59 | |
Nicholas Sauer | f449875 | 2018-02-02 23:52:29 -0800 | [diff] [blame] | 60 | if (savedInstanceState == null) { |
Nicholas Sauer | 61a349c | 2018-04-17 13:11:45 -0700 | [diff] [blame] | 61 | Bundle intentExtras = getIntent().getExtras(); |
| 62 | if (intentExtras != null && intentExtras.getBoolean(EXTRA_RESUME_UPDATE)) { |
| 63 | UpdateLayoutFragment fragment = UpdateLayoutFragment.newResumedInstance(); |
| 64 | getSupportFragmentManager().beginTransaction() |
Nicholas Sauer | 80fcb28 | 2018-05-14 20:28:06 -0700 | [diff] [blame] | 65 | .replace(R.id.device_container, fragment, FRAGMENT_TAG) |
Nicholas Sauer | 61a349c | 2018-04-17 13:11:45 -0700 | [diff] [blame] | 66 | .commitNow(); |
| 67 | } else { |
| 68 | DeviceListFragment fragment = new DeviceListFragment(); |
| 69 | getSupportFragmentManager().beginTransaction() |
Nicholas Sauer | 80fcb28 | 2018-05-14 20:28:06 -0700 | [diff] [blame] | 70 | .replace(R.id.device_container, fragment, FRAGMENT_TAG) |
Nicholas Sauer | 61a349c | 2018-04-17 13:11:45 -0700 | [diff] [blame] | 71 | .commitNow(); |
| 72 | } |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
Nicholas Sauer | 80fcb28 | 2018-05-14 20:28:06 -0700 | [diff] [blame] | 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 |
Nicholas Sauer | 2dc0046 | 2018-02-09 21:58:42 -0800 | [diff] [blame] | 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 |
Nicholas Sauer | 59eeada | 2018-01-31 16:40:37 -0800 | [diff] [blame] | 106 | public void applyUpdate(File file) { |
Nicholas Sauer | f449875 | 2018-02-02 23:52:29 -0800 | [diff] [blame] | 107 | UpdateLayoutFragment fragment = UpdateLayoutFragment.getInstance(file); |
| 108 | getSupportFragmentManager().beginTransaction() |
Nicholas Sauer | 80fcb28 | 2018-05-14 20:28:06 -0700 | [diff] [blame] | 109 | .replace(R.id.device_container, fragment, FRAGMENT_TAG) |
Nicholas Sauer | f449875 | 2018-02-02 23:52:29 -0800 | [diff] [blame] | 110 | .addToBackStack(null) |
| 111 | .commit(); |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 112 | } |
Rakesh Iyer | 3506f3f | 2016-10-19 23:58:48 -0700 | [diff] [blame] | 113 | } |