Skip to content

Instantly share code, notes, and snippets.

View DavidTPate's full-sized avatar

David Pate DavidTPate

  • Denver
View GitHub Profile
Car carSearchCriteria = new Car(1998, null, "Focus", "Sedan");
@DavidTPate
DavidTPate / Car-Builder.java
Created February 16, 2014 22:24
An example of the Builder Pattern in Java for a simple car class.
public class Car {
int year;
String make;
String model;
String carType;
public static class Builder {
int year;
String make;
String model;
@DavidTPate
DavidTPate / Car-Telescoping.java
Created February 16, 2014 21:47
An example of the Telescoping Constructor Anti-Pattern
public class Car {
int year;
String make;
String model;
String carType;
public Car(int year) {
this.year = year;
}
@DavidTPate
DavidTPate / themes.xml
Created April 25, 2013 19:21
Updated themes.xml for AndroidMenuDrawer sample to reduce overdraw and set the window background to null.
<resources>
<style name="SampleBase" parent="@android:style/Theme.Light" />
<style name="SampleTheme" parent="SampleBase">
<item name="android:windowBackground">@null</item>
<item name="menuDrawerStyle">@style/MenuDrawerStyle</item>
</style>
<style name="SampleTheme.RightDrawer" parent="SampleBase">
private void deinitialize() {
stopPreview();
releaseCamera();
Log.d(TAG, "Camera Initialization has Been Undone");
}
private void initialize() {
if (hasCamera(this) &amp;&amp; getCameraInstance() != null) {
startPreview();
Log.d(TAG, "Camera Initialization has Been Completed");
private Camera.ErrorCallback getErrorCallback() {
if (mErrorCallback == null) {
mErrorCallback = new Camera.ErrorCallback() {
@Override
public void onError(int error, Camera camera) {
if (error == Camera.CAMERA_ERROR_SERVER_DIED) {
deinitialize();
initialize();
}
}
private Camera getCameraInstance() {
try {
// attempt to get a Camera instance
// this will open the default camera, which is the back-facing camera (if it exists)
mCamera = Camera.open();
// need to handle the Media Server Dying
mCamera.setErrorCallback(getErrorCallback());
Log.d(TAG, "Camera Opened Successfully");
} catch (RuntimeException e) {
private boolean hasCamera(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
// this device has a camera
Log.d(TAG, "At Least one Camera Detected!");
return true;
} else {
// no camera on this device
Log.d(TAG, "No Cameras detected.");
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_camera_activity);
}
@Override
protected void onPause() {
super.onPause();
deinitialize();
}