Skip to content

Instantly share code, notes, and snippets.

@parahall
parahall / AndroidManifest.xml
Last active December 30, 2015 07:46
Android Academy Weekly Quizz
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.here">
<application
android:name="com.package.here.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
@parahall
parahall / LeakSlackUploadService.java
Created March 19, 2016 14:25
Sending Leak Traces to a Slack Channel using Retrofit2
import com.squareup.leakcanary.AnalysisResult;
import com.squareup.leakcanary.DisplayLeakService;
import com.squareup.leakcanary.HeapDump;
import android.util.Log;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
Imagine that you, and your new Pixel device, have gone back in time to 1980.
Lucky for you, your friend Marty mcFly, the developer of "WatchStarWars" application, and you're just about to watch "Episode V", you're about to watch it for the 1st time.
WatchStarWars app has a single activity, WatchStarwarsActivity.
It's an activity with one button "Say Something", that when clicked, logs an interesting fact about the movie to the log, a fact that, if you haven't watched Episode V, you might find to be a spoiler.
In its OnCreate method, WatchStarwarsActivity posts-delayed a "Finished Watching Starwars" log message for x milliseconds.
Marty loves technology. He uses Timber for injecting a testable log into the activity with Dagger. He is also a very pragmatic developer - he tests his app with Espresso, with this scenario:
Open the activity
@parahall
parahall / ClassWithInnerObject.java
Created September 21, 2016 12:36
Mockito_problem
import android.util.Log;
public class ClassWithInnerObject {
private final InnerObject innerObject;
public ClassWithInnerObject() {
innerObject = new InnerObject();
}
@parahall
parahall / BackgroundService.java
Created February 25, 2017 20:52
BackgroundService.java
public class BackgroundService extends Service {
@Override public int onStartCommand(Intent intent, int i, int i1) {
if (observer == null) {
observer = new OrdersObserver(new Handler());
getContext().getContentResolver()
.registerContentObserver(KolGeneContract.OrderEntry.CONTENT_URI, true, observer);
}
}
@parahall
parahall / KolGeneProvider.java
Last active March 1, 2017 19:10
KolGeneProvider.java
public class KolGeneProvider extends ContentProvider {
//...
@Nullable @Override public Uri insert(@NonNull Uri uri, ContentValues values) {
//open DB for write
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
//match URI to action.
final int match = sUriMatcher.match(uri);
Uri returnUri;
switch (match) {
//case of creating order.
@parahall
parahall / NewOrderPresenter.java
Last active March 9, 2019 17:03
NewOrderPresenter.java
public class NewOrderPresenter extends BasePresenter<NewOrderView> {
//...
private int insertOrder(Order order) {
//turn order to ContentValues object (used by SQL to insert values to Table)
ContentValues values = order.createLocalOrder(order);
//call resolver to insert data to the Order table
Uri uri = context.getContentResolver().insert(KolGeneContract.OrderEntry.CONTENT_URI, values);
//get Id for order.
if (uri != null) {
@parahall
parahall / SendOrderService.java
Created February 25, 2017 20:54
SendOrderService.java
public class SendOrderService extends IntentService {
@Override protected void onHandleIntent(Intent intent) {
int orderId = intent.getIntExtra(ORDER_ID, 0);
if (orderId == 0 || orderId == -1) {
return;
}
Cursor c = null;
try {
@parahall
parahall / SyncOrderService.java
Created February 25, 2017 20:54
SyncOrderService.java
public class SyncOrderService extends GcmTaskService {
//...
public static void scheduleOrderSending(Context context, int id) {
GcmNetworkManager manager = GcmNetworkManager.getInstance(context);
Bundle bundle = new Bundle();
bundle.putInt(SyncOrderService.ORDER_ID, id);
OneoffTask task = new OneoffTask.Builder().setService(SyncOrderService.class)
.setTag(SyncOrderService.getTaskTag(id))
.setExecutionWindow(0L, 30L)
.setExtras(bundle)
public class MainActivity extends LifecycleActivity implements Observer<List<StarWarsMovie>> {
@BindView(R.id.lv_am_movie_list) ListView listView;
@BindView(R.id.pb_am_loading) ProgressBar progressBar;
private MainViewModel mainViewModel;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);