Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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