パーミッションチェックを追加, 使えない機能を非表示に

This commit is contained in:
r-ca 2024-01-23 05:43:40 +09:00
parent 64d7a53616
commit af53ba6c86
No known key found for this signature in database
GPG Key ID: 6A72911AC73464A9
3 changed files with 40 additions and 10 deletions

View File

@ -1,5 +1,6 @@
package one.nem.lacerta;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
@ -11,6 +12,7 @@ import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
@ -85,6 +87,21 @@ public class MainActivity extends AppCompatActivity implements FragmentNavigatio
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "カメラの権限は必須です.", Toast.LENGTH_LONG).show();
finish(); // Exit app
}
}
}
private void initializeApp() {
Log.d("Init", "Initializing app");
// Set feature switch override to default value

View File

@ -1,6 +1,8 @@
package one.nem.lacerta.component.scanner;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
@ -11,6 +13,8 @@ import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
@ -92,6 +96,14 @@ public class ScannerManagerActivity extends AppCompatActivity {
return insets;
});
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1); // TODO-rca: リクエストコードを定数にする
}
MaterialToolbar toolbar = findViewById(R.id.top_toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
@ -116,11 +128,12 @@ public class ScannerManagerActivity extends AppCompatActivity {
Toast.makeText(this, "保存処理", Toast.LENGTH_SHORT).show();
saveNewDocument();
return true;
} else if (item.getItemId() == R.id.action_insert_exist) {
// 既存ドキュメントに挿入
Toast.makeText(this, "Work in progress", Toast.LENGTH_SHORT).show();
insertToExistDocument();
return true;
// }
// else if (item.getItemId() == R.id.action_insert_exist) {
// // 既存ドキュメントに挿入
// Toast.makeText(this, "Work in progress", Toast.LENGTH_SHORT).show();
// insertToExistDocument();
// return true;
} else if (item.getItemId() == android.R.id.home) {
finish();
return true;

View File

@ -6,9 +6,9 @@
android:icon="@drawable/save_24px"
android:title="Save"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_insert_exist"
android:icon="@drawable/ic_baseline_add_24"
android:title="Save"
app:showAsAction="ifRoom"/>
<!-- <item-->
<!-- android:id="@+id/action_insert_exist"-->
<!-- android:icon="@drawable/description_24px"-->
<!-- android:title="Insert"-->
<!-- app:showAsAction="ifRoom"/>-->
</menu>