19. 03. 15
public class PermissionAdapter extends BaseAdapter { private ArrayList permissionItems = new ArrayList<>(); private String[] permissionStrArray; public PermissionAdapter(Context context, String[] permissionStrArray){ this.permissionStrArray = permissionStrArray; for (String permissionStr : permissionStrArray) { Permission permission = new Permission(); switch(permissionStr){ case Manifest.permission.READ_PHONE_STATE: permission.setIcon(context.getResources().getDrawable(R.drawable.baseline_phone_black_18)); permission.setName("전화"); permission.setContent("전화번호를 저장하기 위해 해당 권한이 필요합니다."); break; case Manifest.permission.WRITE_EXTERNAL_STORAGE: permission.setIcon(context.getResources().getDrawable(R.drawable.baseline_storage_black_18)); permission.setName("저장소"); permission.setContent( "프로필 사진을 등록하기 위해 해당 권한이 필요합니다."); break; case Manifest.permission.RECORD_AUDIO: permission.setIcon(context.getResources().getDrawable(R.drawable.baseline_record_voice_over_black_18)); permission.setName("음성 녹음"); permission.setContent( "음성 데이터 활용을 위해 해당 권한이 필요합니다."); break; } permissionItems.add(permission); } } @Override public int getCount() { return permissionItems.size(); } @Override public Permission getItem(int position) { return permissionItems.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { Context context = parent.getContext(); if(convertView == null){ LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_view_permission, parent, false); } ImageView mIvPermission = convertView.findViewById(R.id.iv_permission); TextView mTvPermissionName = convertView.findViewById(R.id.tv_permission_name); TextView mTvPermissionContent = convertView.findViewById(R.id.tv_permission_content); CheckBox mCbPermissionCheck = convertView.findViewById(R.id.cb_permission_check); PermissionListener permissionListener = new PermissionListener() { @Override public void onPermissionGranted() { mCbPermissionCheck.setClickable(false); if(permissionCheck(context)) { Intent intent = new Intent(context, CertificationActivity.class); context.startActivity(intent); ((Activity)context).finish(); } } @Override public void onPermissionDenied(List deniedPermissions) { mCbPermissionCheck.setChecked(false); } }; int permissionCheck = ContextCompat.checkSelfPermission(context, permissionStrArray[position]); if(permissionCheck == PackageManager.PERMISSION_DENIED){ mCbPermissionCheck.setOnClickListener(v -> showPermission(context, permissionListener, permissionStrArray[position])); }else{ mCbPermissionCheck.setChecked(true); mCbPermissionCheck.setClickable(false); } Permission permission = permissionItems.get(position); Glide.with(context) .load(permission.getIcon()) .into(mIvPermission); mTvPermissionName.setText(permission.getName()); mTvPermissionContent.setText(permission.getContent()); return convertView; } private void showPermission(Context context, PermissionListener permissionListener, String permission){ TedPermission.with(context) .setPermissionListener(permissionListener) .setDeniedMessage("모든 권한이 승인되지 않은 경우, 앱을 사용할 수 없습니다..") .setPermissions(permission) .check(); } private boolean permissionCheck(Context context){ for(String permissionStr : permissionStrArray) { int permissionCheck = ContextCompat.checkSelfPermission(context, permissionStr); if (permissionCheck == PackageManager.PERMISSION_DENIED) { return false; } } return true; } }
2.Model
public class Permission { private Drawable icon; private String name; private String content; public Drawable getIcon() { return icon; } public void setIcon(Drawable icon) { this.icon = icon; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
3. xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:background="@color/white"> <LinearLayout android:id="@+id/ll_wrap" android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal"> <ImageView android:id="@+id/iv_permission" android:layout_width="30dp" android:layout_height="match_parent"/> <LinearLayout android:weightSum="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/tv_permission_name" android:layout_width="70dp" android:layout_height="wrap_content" android:layout_weight="0.2"/> <TextView android:id="@+id/tv_permission_content" android:layout_width="170dp" android:layout_height="wrap_content" android:layout_weight="0.6"/> <CheckBox android:id="@+id/cb_permission_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.2"/> </LinearLayout> </LinearLayout> </LinearLayout>
public class PermissionActivity extends Activity{ private static final String[] permissionArr = new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO}; @BindView(R.id.lv_permission) ListView mLvPermission; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_permission); ButterKnife.bind(this); PermissionAdapter permissionAdapter = new PermissionAdapter(this, permissionArr); mLvPermission.setAdapter(permissionAdapter); } }
5. 결과
'Before 2022 > Android' 카테고리의 다른 글
ViewPager에서 Fragment의 함수 호출 방법 (0) | 2019.01.24 |
---|---|
Weak Reference에 대해(메모리 누수) (0) | 2019.01.22 |
EditText 제어 (0) | 2019.01.16 |
Fragment 다루기 (0) | 2019.01.15 |
안드로이드 입력 키보드 hide/show (0) | 2019.01.15 |