• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     };
44 
45     @Override
onCreate(Bundle savedInstanceState)46     protected void onCreate(Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48 
49         if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
50                 != PackageManager.PERMISSION_GRANTED) {
51             ActivityCompat.requestPermissions(this, REQUIRED_STORAGE_PERMISSIONS,
52                     STORAGE_PERMISSIONS_REQUEST_CODE);
53         }
54 
55         setContentView(R.layout.activity_main);
56         Toolbar toolbar = findViewById(R.id.toolbar);
57         setSupportActionBar(toolbar);
58 
59         if (savedInstanceState == null) {
60             Bundle intentExtras = getIntent().getExtras();
61             if (intentExtras != null && intentExtras.getBoolean(EXTRA_RESUME_UPDATE)) {
62                 UpdateLayoutFragment fragment = UpdateLayoutFragment.newResumedInstance();
63                 getSupportFragmentManager().beginTransaction()
64                         .replace(R.id.device_container, fragment, FRAGMENT_TAG)
65                         .commitNow();
66             } else {
67                 DeviceListFragment fragment = new DeviceListFragment();
68                 getSupportFragmentManager().beginTransaction()
69                         .replace(R.id.device_container, fragment, FRAGMENT_TAG)
70                         .commitNow();
71             }
72         }
73     }
74 
75     @Override
onOptionsItemSelected(MenuItem item)76     public boolean onOptionsItemSelected(MenuItem item) {
77         if (item.getItemId() == android.R.id.home) {
78             UpFragment upFragment =
79                     (UpFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
80             if (!upFragment.goUp()) {
81                 onBackPressed();
82             }
83             return true;
84         }
85         return super.onOptionsItemSelected(item);
86     }
87 
88     @Override
onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)89     public void onRequestPermissionsResult(int requestCode, String permissions[],
90             int[] grantResults) {
91         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
92         if (STORAGE_PERMISSIONS_REQUEST_CODE == requestCode) {
93             if (grantResults.length == 0) {
94                 finish();
95             }
96             for (int grantResult : grantResults) {
97                 if (grantResult != PackageManager.PERMISSION_GRANTED) {
98                     finish();
99                 }
100             }
101         }
102     }
103 
104     @Override
applyUpdate(File file)105     public void applyUpdate(File file) {
106         UpdateLayoutFragment fragment = UpdateLayoutFragment.getInstance(file);
107         getSupportFragmentManager().beginTransaction()
108                 .replace(R.id.device_container, fragment, FRAGMENT_TAG)
109                 .addToBackStack(null)
110                 .commit();
111     }
112 }
113