Skip to content

Instantly share code, notes, and snippets.

@abombss
Created February 25, 2011 19:16
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save abombss/844325 to your computer and use it in GitHub Desktop.
Save abombss/844325 to your computer and use it in GitHub Desktop.
A roboguice provider for creating a User-Agent string on android
public class MyModule extends AbstractModule {
@Override
void configure() {
bind(String.class).annotatedWith(UserAgent.class).to(UserAgentProvider.class);
}
}
public class MyComponent {
String userAgent;
@Inject
public MyComponent(@UserAgent String userAgent) {
this.userAgent = userAgent;
}
}
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
public @interface UserAgent {}
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.Build;
import android.util.DisplayMetrics;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.util.Locale;
@Singleton
public class UserAgentProvider implements Provider<String> {
Context application;
volatile String userAgent;
@Inject
public UserAgentProvider(Context application) {
this.application = application.getApplicationContext();
}
@Override
public String get() {
if (userAgent == null) {
synchronized (UserAgentProvider.class) {
if (userAgent == null) {
userAgent = createUserAgentString();
}
}
}
return userAgent;
}
private String createUserAgentString() {
String appName = "";
String appVersion = "";
int height = 0;
int width = 0;
DisplayMetrics display = application.getResources().getDisplayMetrics();
Configuration config = application.getResources().getConfiguration();
// Always send screen dimension for portrait mode
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
height = display.widthPixels;
width = display.heightPixels;
} else {
width = display.widthPixels;
height = display.heightPixels;
}
try {
PackageInfo packageInfo = application.getPackageManager().getPackageInfo(application.getPackageName(), PackageManager.GET_CONFIGURATIONS);
appName = packageInfo.packageName;
appVersion = packageInfo.versionName;
} catch (PackageManager.NameNotFoundException ignore) {
// this should never happen, we are looking up ourself
}
// Tries to conform to default android UA string without the Safari / webkit noise, plus adds the screen dimensions
return String.format("%1$s/%2$s (%3$s; U; Android %4$s; %5$s-%6$s; %12$s Build/%7$s; %8$s) %9$dX%10$d %11$s %12$s"
, appName
, appVersion
, System.getProperty("os.name", "Linux")
, Build.VERSION.RELEASE
, config.locale.getLanguage().toLower()
, config.locale.getCountry().toLower()
, Build.ID
, Build.BRAND
, width
, height
, Build.MANUFACTURER
, Build.MODEL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment