Skip to content

Instantly share code, notes, and snippets.

@jesselima
Forked from gabrielemariotti/HelperUtil.java
Created October 15, 2018 09:24
Show Gist options
  • Save jesselima/66578a56d51d8ac3d88a870cb845b6a7 to your computer and use it in GitHub Desktop.
Save jesselima/66578a56d51d8ac3d88a870cb845b6a7 to your computer and use it in GitHub Desktop.
HelperUtil using a IMPL
public class HelperUtil {
private final HelperUtilImpl mImpl;
public HelperUtil (Context context) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
mImpl = new HelperUtilImplL(context);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
mImpl = new HelperUtilImplKK(context);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mImpl = new HelperUtilImplJB(context);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mImpl = new HelperUtilImplICS(context);
} else {
mImpl = new HelperUtilImplBase(context);
}
}
public void setActionBar() {
mImpl.setActionBar();
}
public void setElevation(View view, float elevation){
mImpl.setElevation(view, elevation);
}
}
public interface HelperUtilImpl {
public void setActionBar();
public void setElevation(View view, float elevation);
}
public class HelperUtilImplBase implements HelperUtilImpl{
protected Context mContext;
public HelperUtilImplBase(Context context){
this.mContext = context;
}
public void setActionBar() {
// Do nothing pre-L
}
public void setElevation(View view, float elevation){
// Do nothing pre-L
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public class HelperUtilImplKK extends HelperUtilImplBase{
public HelperUtilImplKK(Context context){
super(context);
}
}
@TargetApi(Build.VERSION_CODES.L)
public class HelperUtilImplL extends HelperUtilImplBase{
private Toolbar mActionBarToolbar;
public HelperUtilImplL(Context context){
super(context);
}
@Override
public void setElevation(View view, float elevation){
view.setElevation(elevation);
}
@Override
public void setActionBar() {
mActionBarToolbar = (Toolbar) ((Activity)mContext).findViewById(R.id.toolbar);
if (mActionBarToolbar != null) {
((Activity)mContext).setActionBar(mActionBarToolbar);
}
}
}
public class SimpleActivity extends Activity {
HelperUtil helper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helper = new HelperUtil(this);
//........
helper.setElevation(view, 2.0f);
helper.setActionBar();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment