-
-
Save jesselima/66578a56d51d8ac3d88a870cb845b6a7 to your computer and use it in GitHub Desktop.
HelperUtil using a IMPL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface HelperUtilImpl { | |
public void setActionBar(); | |
public void setElevation(View view, float elevation); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@TargetApi(Build.VERSION_CODES.KITKAT) | |
public class HelperUtilImplKK extends HelperUtilImplBase{ | |
public HelperUtilImplKK(Context context){ | |
super(context); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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