Skip to content

Instantly share code, notes, and snippets.

View Amokrane's full-sized avatar
🎯
Focusing

Amokrane Chentir Amokrane

🎯
Focusing
View GitHub Profile
@Amokrane
Amokrane / worker_notifs.rb
Created April 9, 2012 10:20
Worker code for notifs
require 'iron_worker'
class C2dmIronWorker < IronWorker::Base
attr_accessor :auth_token
attr_accessor :c2dms
attr_accessor :params
def run
uri = URI.parse('https://android.clients.google.com/c2dm/send')
@Amokrane
Amokrane / iron_worker_c2dms.rb
Created April 9, 2012 09:24
Using Iron.IO for pushing notifications
require 'iron_worker'
class NotifsPusher
def perform
begin
puts "Initializing IronWorker and pushing billions of C2dm into it..."
IronWorker.configure do |config|
config.token = ENV['IRON_WORKER_TOKEN']
config.project_id = ENV['IRON_WORKER_PROJECT_ID']
end
@Amokrane
Amokrane / custom_internal_mem_android_emulator.sh
Created February 24, 2012 10:01
Start an Android emulator with custom internal memory
emulator-arm.exe -partition-size 123 -avd myandroid4
@Amokrane
Amokrane / ab_testing.java
Created February 3, 2012 13:04
AB Testing on Android
private static final boolean isA = UUID.randomUUID().getLeastSignificantBits() % 2 == 0;
@Override
public void onCreate(...) {
super.onCreate(saved...);
if(!isA) {
setContentView(R.layout.mainA);
MyApp.getInstance().tracker().trackPageView("/AUser");
} else {
@Amokrane
Amokrane / parallel_pattern.java
Created February 3, 2012 12:57
The parallel activity pattern
private static boolean shinyNewAPIS = android.os.Build.VERSION_SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent startActivityIntent = null;
if(!shinyNewAPIS) {
startActivityIntent = new Intent(this, legacyActivity.class);
@Amokrane
Amokrane / allocation_tracking.java
Created January 31, 2012 22:15
Allocation Tracking
// Limit allocations to find problems
int prevLimit = -1;
try {
prevLimit = Debug.setAllocationLimit(0);
// Do stuff
} finally {
Debug.setAllocationLimit(-1);
}
@Amokrane
Amokrane / hit_and_run.java
Created January 30, 2012 23:22
Pattern for writing animations
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotationY", 180);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setLayerType(View.LAYER_TYPE_NONE, null);
}
});
@Amokrane
Amokrane / decode_bitmap.java
Created January 30, 2012 22:51
Clever way to decode a bitmap
// Allocate a bitmap of the desired size
Bitmap b = Bitmap.createBitmap(256, 256, BitmaP.Config.ARGB_8888);
// Set the allocated bitmap as the target
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inBitmap = b;
// Decode resource in pre-allocated bitmap
BitmapFactory.decodeResource(resources, R.drawable.texture, opts);
@Amokrane
Amokrane / copyView.java
Created January 26, 2012 12:52
Copy a view into a Bitmap (useful for taking screenshots)
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNDEFINED);
view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.translate(-view.getScrollX(), -view.getScrollY());
view.draw(c);
// OR
view.setDrawingCacheEnabled(true);
@Amokrane
Amokrane / draw_canvas.java
Created January 26, 2012 12:51
Load a (mutable) bitmap into a Canvas and write text over it
Paint p = new Paint();
p.setAntiAlias(true);
p.setTextSize(24);
Bitmap b = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawText("Devoxx", 0.0f, 128.0f, p);