Skip to content

Instantly share code, notes, and snippets.

@michaeimm
Last active January 16, 2018 09:21
Show Gist options
  • Save michaeimm/13aebc2a42f8870b4983dbf7ff3de177 to your computer and use it in GitHub Desktop.
Save michaeimm/13aebc2a42f8870b4983dbf7ff3de177 to your computer and use it in GitHub Desktop.
RequestPermissionsUtil
/*
Copyright 2017 少年ウィンド
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tw.shounenwind.healingplurk.fragments;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import tw.shounenwind.healingplurk.utils.RequestPermissionsUtil;
public class RequestPermissionsFragment extends Fragment {
private RequestPermissionsUtil.OnGrant onGrant;
private RequestPermissionsUtil.OnDeny onDeny;
private RequestPermissionsUtil.OnFinish onFinish;
public void setOnGrantListener(RequestPermissionsUtil.OnGrant onGrant) {
this.onGrant = onGrant;
}
public void setOnDenyListener(RequestPermissionsUtil.OnDeny onDeny) {
this.onDeny = onDeny;
}
public void setOnFinishListener(RequestPermissionsUtil.OnFinish onFinish) {
this.onFinish = onFinish;
}
public void runWithPermissionCheck(@NonNull String[] permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean needRequest = false;
List<String> requirePermissions = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(getContext(), permission) != PackageManager.PERMISSION_GRANTED) {
requirePermissions.add(permission);
needRequest = true;
}
}
if (needRequest) {
requestPermissions(requirePermissions.toArray(new String[requirePermissions.size()]), 0);
return;
}
}
if (onGrant != null)
onGrant.run();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.d("permission", "onRequestPermissionsResult: " + requestCode);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode != 0)
return;
boolean allPermissionGranted = true;
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
allPermissionGranted = false;
break;
}
}
if (allPermissionGranted) {
if (onGrant != null)
onGrant.run();
} else {
if (onDeny != null) {
onDeny.run();
}
}
onFinish.run();
}
}
/*
Copyright 2017 少年ウィンド
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tw.shounenwind.healingplurk.utils;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import tw.shounenwind.healingplurk.fragments.RequestPermissionsFragment;
public class RequestPermissionsUtil {
private static final String TAG = "RequestPermissionsUtil";
private final List<String> permissions;
private OnGrant onGrant;
private OnDeny onDeny;
private WeakReference<AppCompatActivity> wrActivity;
private RequestPermissionsFragment fragment;
public RequestPermissionsUtil() {
permissions = new ArrayList<>();
}
public RequestPermissionsUtil addPermissions(String... permissions) {
this.permissions.addAll(Arrays.asList(permissions));
return this;
}
public RequestPermissionsUtil setOnGrant(OnGrant onGrant) {
this.onGrant = onGrant;
return this;
}
public RequestPermissionsUtil setOnDeny(OnDeny onDeny) {
this.onDeny = onDeny;
return this;
}
public void request(AppCompatActivity activity) {
wrActivity = new WeakReference<>(activity);
fragment = new RequestPermissionsFragment();
fragment.setOnGrantListener(onGrant);
fragment.setOnDenyListener(onDeny);
fragment.setOnFinishListener(new OnFinish());
FragmentManager fragmentManager = activity.getSupportFragmentManager();
fragmentManager
.beginTransaction()
.add(fragment, TAG)
.commitNowAllowingStateLoss();
fragmentManager.executePendingTransactions();
fragment.runWithPermissionCheck(permissions.toArray(new String[permissions.size()]));
}
public interface OnGrant {
void run();
}
public interface OnDeny {
void run();
}
public class OnFinish {
public void run() {
if (wrActivity == null || fragment == null)
return;
AppCompatActivity activity = wrActivity.get();
if (activity == null)
return;
FragmentManager fragmentManager = activity.getSupportFragmentManager();
fragmentManager
.beginTransaction()
.remove(fragment)
.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment