Skip to content

Instantly share code, notes, and snippets.

@lokeshsuryan
Created October 1, 2021 06:57
Show Gist options
  • Save lokeshsuryan/53e531e0990394f77599914ad9ed4f67 to your computer and use it in GitHub Desktop.
Save lokeshsuryan/53e531e0990394f77599914ad9ed4f67 to your computer and use it in GitHub Desktop.
package ohos.samples.camera;
import ohos.samples.camera.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;
import java.util.Arrays;
/**
* MainAbility
*/
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
requestPermissions();
}
private void requestPermissions() {
String[] permissions = {
SystemPermission.WRITE_USER_STORAGE, SystemPermission.READ_USER_STORAGE, SystemPermission.CAMERA,
SystemPermission.MICROPHONE, SystemPermission.LOCATION
};
requestPermissionsFromUser(Arrays.stream(permissions)
.filter(permission -> verifySelfPermission(permission) != IBundleManager.PERMISSION_GRANTED).toArray(String[]::new), 0);
}
@Override
public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
if (permissions == null || permissions.length == 0 || grantResults == null || grantResults.length == 0) {
return;
}
for (int grantResult : grantResults) {
if (grantResult != IBundleManager.PERMISSION_GRANTED) {
terminateAbility();
break;
}
}
}
}
Create MainAbilitySlice.java ability and add the below code.
package ohos.samples.camera.slice;
import ohos.samples.camera.ResourceTable;
import ohos.samples.camera.TakePhotoAbility;
import ohos.samples.camera.VideoRecordAbility;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
/**
* MainAbilitySlice
*/
public class MainAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_main_ability_slice);
initComponents();
}
private void initComponents() {
Component takePhoto = findComponentById(ResourceTable.Id_take_photo);
Component videoRecord = findComponentById(ResourceTable.Id_video_record);
takePhoto.setClickedListener((component) -> startAbility(TakePhotoAbility.class.getName()));
videoRecord.setClickedListener((component) -> startAbility(VideoRecordAbility.class.getName()));
}
private void startAbility(String abilityName) {
Operation operation = new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName(getBundleName())
.withAbilityName(abilityName)
.build();
Intent intent = new Intent();
intent.setOperation(operation);
startAbility(intent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment