+ *
* note: if the {@link PermissionModel#isCanSkip()} return false, we could display the explanation immediately. */ protected abstract void onUserDeclinePermission(String permissionName); - @Override - protected void onSaveInstanceState(Bundle outState) { + @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (pager != null) { outState.putInt(PAGER_POSITION, pager.getCurrentItem()); @@ -80,8 +75,7 @@ protected void onSaveInstanceState(Bundle outState) { outState.putInt(SYSTEM_OVERLAY_NUM_INSTANCE, systemOverRequestNumber); } - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { if (theme() != 0) setTheme(theme()); super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); @@ -110,11 +104,7 @@ public void onPageSelected(int position) { animateColorChange(pager, color); } }); - if (pagerTransformer() == null) - pager.setPageTransformer(true, new IntroTransformer()); - else - pager.setPageTransformer(true, pagerTransformer()); - + pager.setPageTransformer(true, pagerTransformer() == null ? new IntroTransformer() : pagerTransformer()); if (savedInstanceState != null) { pager.setCurrentItem(savedInstanceState.getInt(PAGER_POSITION), true); systemOverRequestNumber = savedInstanceState.getInt(SYSTEM_OVERLAY_NUM_INSTANCE); @@ -123,28 +113,25 @@ public void onPageSelected(int position) { /** * Used to determine if the user accepted {@link android.Manifest.permission#SYSTEM_ALERT_WINDOW} or no. - *+ *
* if you never passed the permission this method won't be called. */ - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { permissionHelper.onActivityForResult(requestCode); + super.onActivityResult(requestCode, resultCode, data); } - @Override - public void onBackPressed() { + @Override public void onBackPressed() { if (backPressIsEnabled()) { super.onBackPressed(); } } - @Override - public void onPermissionGranted(String[] permissionName) { + @Override public void onPermissionGranted(@NonNull String[] permissionName) { onNext(permissionName[0]);// we are certain that one permission is requested. } - @Override - public void onPermissionDeclined(String[] permissionName) { + @Override public void onPermissionDeclined(@NonNull String[] permissionName) { PermissionModel model = getPermission(pager.getCurrentItem()); if (model != null) { if (!model.isCanSkip()) { @@ -167,13 +154,11 @@ public void onPermissionDeclined(String[] permissionName) { onNext(permissionName[0]); } - @Override - public void onPermissionPreGranted(String permissionsName) { + @Override public void onPermissionPreGranted(@NonNull String permissionsName) { onNext(permissionsName); } - @Override - public void onPermissionNeedExplanation(String permissionName) { + @Override public void onPermissionNeedExplanation(@NonNull String permissionName) { if (!permissionName.equalsIgnoreCase(Manifest.permission.SYSTEM_ALERT_WINDOW)) { PermissionModel model = getPermission(pager.getCurrentItem()); if (model != null) { @@ -186,14 +171,12 @@ public void onPermissionNeedExplanation(String permissionName) { } } - @Override - public void onPermissionReallyDeclined(String permissionName) { + @Override public void onPermissionReallyDeclined(@NonNull String permissionName) { permissionIsPermanentlyDenied(permissionName); onNoPermissionNeeded(); } - @Override - public void onNoPermissionNeeded() { + @Override public void onNoPermissionNeeded() { if ((pager.getAdapter().getCount() - 1) == pager.getCurrentItem()) { onIntroFinished(); } else { @@ -201,8 +184,7 @@ public void onNoPermissionNeeded() { } } - @Override - public void onStatusBarColorChange(@ColorInt int color) { + @Override public void onStatusBarColorChange(@ColorInt int color) { if (color == 0) return; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { float cl = 0.9f; @@ -215,23 +197,21 @@ public void onStatusBarColorChange(@ColorInt int color) { } } - @Override - public void onSkip(@NonNull String permissionName) { + @Override public void onSkip(@NonNull String permissionName) { pager.setCurrentItem(pager.getCurrentItem() - 1, true); } - @Override - public void onNext(@NonNull String permissionName) { + @Override public void onNext(@NonNull String permissionName) { int currentPosition = pager.getCurrentItem(); if ((pager.getAdapter().getCount() - 1) == (currentPosition)) { onNoPermissionNeeded(); } else { - pager.setCurrentItem(pager.getCurrentItem() + 1, true); + currentPosition = pager.getCurrentItem() + 1; + pager.setCurrentItem(currentPosition, true); } } - @Override - public void onPermissionRequest(@NonNull String permissionName, boolean canSkip) { + @Override public void onPermissionRequest(@NonNull String permissionName, boolean canSkip) { if (permissionHelper.isExplanationNeeded(permissionName)) { onPermissionNeedExplanation(permissionName); } else { @@ -239,21 +219,20 @@ public void onPermissionRequest(@NonNull String permissionName, boolean canSkip) } } - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults); } /** * @return instance of {@link PermissionFragment} */ - protected PermissionFragment getFragment(int index) { + @SuppressWarnings("unused") protected PermissionFragment getFragment(int index) { return (PermissionFragment) pager.getAdapter().instantiateItem(pager, index); } /** * return PermissionModel at specific index. - *+ *
* if index > {@link #permissions().size()} null will be returned */ protected PermissionModel getPermission(int index) { diff --git a/permission/src/main/java/com/fastaccess/permission/base/callback/OnPermissionCallback.java b/permission/src/main/java/com/fastaccess/permission/base/callback/OnPermissionCallback.java index 3e1583d..f5b4c11 100644 --- a/permission/src/main/java/com/fastaccess/permission/base/callback/OnPermissionCallback.java +++ b/permission/src/main/java/com/fastaccess/permission/base/callback/OnPermissionCallback.java @@ -1,16 +1,18 @@ package com.fastaccess.permission.base.callback; +import android.support.annotation.NonNull; + public interface OnPermissionCallback { - void onPermissionGranted(String[] permissionName); + void onPermissionGranted(@NonNull String[] permissionName); - void onPermissionDeclined(String[] permissionName); + void onPermissionDeclined(@NonNull String[] permissionName); - void onPermissionPreGranted(String permissionsName); + void onPermissionPreGranted(@NonNull String permissionsName); - void onPermissionNeedExplanation(String permissionName); + void onPermissionNeedExplanation(@NonNull String permissionName); - void onPermissionReallyDeclined(String permissionName); + void onPermissionReallyDeclined(@NonNull String permissionName); void onNoPermissionNeeded(); } diff --git a/permission/src/main/java/com/fastaccess/permission/base/fragment/PermissionFragment.java b/permission/src/main/java/com/fastaccess/permission/base/fragment/PermissionFragment.java index 14c017b..1c139ce 100644 --- a/permission/src/main/java/com/fastaccess/permission/base/fragment/PermissionFragment.java +++ b/permission/src/main/java/com/fastaccess/permission/base/fragment/PermissionFragment.java @@ -40,8 +40,7 @@ public static PermissionFragment newInstance(PermissionModel permissionModel) { return fragment; } - @Override - public void onAttach(Context context) { + @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof BaseCallback) { callback = (BaseCallback) context; @@ -50,28 +49,23 @@ public void onAttach(Context context) { } } - @Override - public void onDetach() { + @Override public void onDetach() { super.onDetach(); callback = null; } - @Override - public void onSaveInstanceState(Bundle outState) { + @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (permissionModel != null) { outState.putParcelable(PERMISSION_INSTANCE, permissionModel); } } - @Nullable - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_layout, container, false); } - @Override - public void onViewCreated(View view, Bundle savedInstanceState) { + @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); if (savedInstanceState != null) { permissionModel = savedInstanceState.getParcelable(PERMISSION_INSTANCE); @@ -93,11 +87,25 @@ public void onViewCreated(View view, Bundle savedInstanceState) { initViews(); } + @Override public void onClick(View v) { + if (v.getId() == R.id.previous) { + callback.onSkip(permissionModel.getPermissionName()); + } else if (v.getId() == R.id.next) { + if (!permissionModel.isCanSkip()) { + callback.onPermissionRequest(permissionModel.getPermissionName(), false); + } else { + callback.onNext(permissionModel.getPermissionName()); + } + } else if (v.getId() == R.id.request) { + callback.onPermissionRequest(permissionModel.getPermissionName(), true); + } + } + private void initViews() { request.setVisibility(Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? View.GONE : View.VISIBLE); image.setImageResource(permissionModel.getImageResourceId()); title.setText(permissionModel.getTitle()); - title.setTextSize(permissionModel.getTextSize()); + title.setTextSize(TypedValue.COMPLEX_UNIT_PX, permissionModel.getTextSize()); title.setTextColor(permissionModel.getTextColor() == 0 ? Color.WHITE : permissionModel.getTextColor()); message.setText(permissionModel.getMessage()); message.setTextColor(permissionModel.getTextColor() == 0 ? Color.WHITE : permissionModel.getTextColor()); @@ -113,19 +121,4 @@ private void initViews() { } } } - - @Override - public void onClick(View v) { - if (v.getId() == R.id.previous) { - callback.onSkip(permissionModel.getPermissionName()); - } else if (v.getId() == R.id.next) { - if (!permissionModel.isCanSkip()) { - callback.onPermissionRequest(permissionModel.getPermissionName(), false); - } else { - callback.onNext(permissionModel.getPermissionName()); - } - } else if (v.getId() == R.id.request) { - callback.onPermissionRequest(permissionModel.getPermissionName(), true); - } - } } diff --git a/permission/src/main/java/com/fastaccess/permission/base/model/PermissionModelBuilder.java b/permission/src/main/java/com/fastaccess/permission/base/model/PermissionModelBuilder.java index 8c792cd..10dd5a2 100644 --- a/permission/src/main/java/com/fastaccess/permission/base/model/PermissionModelBuilder.java +++ b/permission/src/main/java/com/fastaccess/permission/base/model/PermissionModelBuilder.java @@ -1,30 +1,27 @@ package com.fastaccess.permission.base.model; import android.content.Context; -import android.content.res.Resources; import android.graphics.Color; -import android.os.Build; import android.support.annotation.ColorInt; import android.support.annotation.ColorRes; import android.support.annotation.DimenRes; import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; import android.support.annotation.StringRes; +import android.support.v4.app.ActivityCompat; import com.fastaccess.permission.R; public class PermissionModelBuilder { - private final Resources res; - private Resources.Theme theme; private final PermissionModel permissionModel; + private Context context; private PermissionModelBuilder(@NonNull Context context) { - this.res = context.getResources(); - this.theme = context.getTheme(); + this.context = context; this.permissionModel = new PermissionModel(); // Generate sane default values withTextColor(Color.WHITE); - withTextSize(res.getDimensionPixelSize(R.dimen.permissions_text_size)); + withTextSize(context.getResources().getDimensionPixelSize(R.dimen.permissions_text_size)); withRequestIcon(R.drawable.ic_arrow_done); withPreviousIcon(R.drawable.ic_arrow_left); withNextIcon(R.drawable.ic_arrow_right); @@ -54,11 +51,7 @@ public PermissionModelBuilder withLayoutColor(@ColorInt int layoutColor) { } public PermissionModelBuilder withLayoutColorRes(@ColorRes int layoutColor) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - this.permissionModel.setLayoutColor(res.getColor(layoutColor, theme)); - } else { - this.permissionModel.setLayoutColor(res.getColor(layoutColor)); - } + this.permissionModel.setLayoutColor(ActivityCompat.getColor(context, layoutColor)); return this; } @@ -68,11 +61,7 @@ public PermissionModelBuilder withTextColor(@ColorInt int textColor) { } public PermissionModelBuilder withTextColorRes(@ColorRes int textColor) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - this.permissionModel.setTextColor(res.getColor(textColor, theme)); - } else { - this.permissionModel.setTextColor(res.getColor(textColor)); - } + this.permissionModel.setTextColor(ActivityCompat.getColor(context, textColor)); return this; } @@ -87,7 +76,7 @@ public PermissionModelBuilder withExplanationMessage(@NonNull String explanation } public PermissionModelBuilder withExplanationMessage(@StringRes int explanationMessage) { - this.permissionModel.setExplanationMessage(res.getString(explanationMessage)); + this.permissionModel.setExplanationMessage(context.getString(explanationMessage)); return this; } @@ -117,7 +106,7 @@ public PermissionModelBuilder withMessage(@NonNull String message) { } public PermissionModelBuilder withMessage(@StringRes int message) { - this.permissionModel.setMessage(res.getString(message)); + this.permissionModel.setMessage(context.getString(message)); return this; } @@ -127,7 +116,7 @@ public PermissionModelBuilder withTitle(@NonNull String title) { } public PermissionModelBuilder withTitle(@StringRes int title) { - this.permissionModel.setTitle(res.getString(title)); + this.permissionModel.setTitle(context.getString(title)); return this; } diff --git a/permission/src/main/java/com/fastaccess/permission/base/utils/ThemeUtil.java b/permission/src/main/java/com/fastaccess/permission/base/utils/ThemeUtil.java index c367fd6..95ce137 100644 --- a/permission/src/main/java/com/fastaccess/permission/base/utils/ThemeUtil.java +++ b/permission/src/main/java/com/fastaccess/permission/base/utils/ThemeUtil.java @@ -11,15 +11,13 @@ */ public class ThemeUtil { - @ColorInt - public static int getPrimaryColor(Context context) { + @ColorInt public static int getPrimaryColor(Context context) { TypedValue value = new TypedValue(); context.getTheme().resolveAttribute(R.attr.colorPrimary, value, true); return value.data; } - @ColorInt - public static int getThemeAccentColor(Context context) { + @ColorInt public static int getThemeAccentColor(Context context) { TypedValue value = new TypedValue(); context.getTheme().resolveAttribute(R.attr.colorAccent, value, true); return value.data; diff --git a/permission/src/main/res/layout-land/fragment_layout.xml b/permission/src/main/res/layout-land/fragment_layout.xml index c3fd2ae..3e4da4b 100644 --- a/permission/src/main/res/layout-land/fragment_layout.xml +++ b/permission/src/main/res/layout-land/fragment_layout.xml @@ -52,7 +52,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" - android:layout_marginBottom="10dip" + android:layout_marginBottom="10dp" android:gravity="center" android:autoLink="all" android:maxLines="4"/> diff --git a/permission/src/test/java/com/fastaccess/permission/ExampleUnitTest.java b/permission/src/test/java/com/fastaccess/permission/ExampleUnitTest.java deleted file mode 100644 index d602a7f..0000000 --- a/permission/src/test/java/com/fastaccess/permission/ExampleUnitTest.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.fastaccess.permission; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * To work on unit tests, switch the Test Artifact in the Build Variants view. - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() throws Exception { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file