Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavpandey committed Jan 17, 2018
1 parent 92cffbf commit 045567f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.pranavpandey.android.dynamic.engine.listener;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.pranavpandey.android.dynamic.engine.model.DynamicAppInfo;
Expand All @@ -35,9 +34,9 @@ public interface DynamicEventListener {
* charging, headset and dock state.
*
* @param charging {@code true} if the device is charging or connected
* to a power source.
* to a power source.
* @param headset {@code true} if the device is connected to a headset
* or a audio output device.
* or a audio output device.
* @param docked {@code true} if the device is docked.
*/
void onInitialize(boolean charging, boolean headset, boolean docked);
Expand All @@ -46,7 +45,7 @@ public interface DynamicEventListener {
* On call state changed. Either on call or the device is idle.
*
* @param call {@code true} if the device is on call. Either ringing
* or answered.
* or answered.
*/
void onCallStateChange(boolean call);

Expand All @@ -55,7 +54,7 @@ public interface DynamicEventListener {
* state independent of the PIN, password or any other security lock.
*
* @param locked {@code true} if the device is in the locked state or
* the lock screen is shown.
* the lock screen is shown.
*/
void onLockStateChange(boolean locked);

Expand All @@ -64,7 +63,7 @@ public interface DynamicEventListener {
* output device or volume is routed through the internal speaker.
*
* @param connected {@code true} if the device is connected to a headset
* or a audio output device.
* or a audio output device.
*/
void onHeadsetStateChange(boolean connected);

Expand All @@ -73,7 +72,7 @@ public interface DynamicEventListener {
* source using the battery.
*
* @param charging {@code true} if the device is charging or connected
* to a power source.
* to a power source.
*/
void onChargingStateChange(boolean charging);

Expand All @@ -96,8 +95,9 @@ public interface DynamicEventListener {
* On app package added or changed. Useful to show a notification if
* an app is updated or a new app is installed.
*
* @param dynamicAppInfo {@link DynamicAppInfo} of the updated or added package.
* @param dynamicAppInfo {@link DynamicAppInfo} of the updated or added
* package.
* @param newPackage {@code true} if the package is newly added.
*/
void onPackageUpdated(@NonNull DynamicAppInfo dynamicAppInfo, boolean newPackage);
void onPackageUpdated(@Nullable DynamicAppInfo dynamicAppInfo, boolean newPackage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
* Collection of various properties for a given package for an easy
Expand Down Expand Up @@ -49,7 +50,7 @@ public class DynamicAppInfo implements Parcelable {
private String label;

/**
* Default constructor to initialize DynamicAppInfo.
* Default constructor to initialize the dynamic app info.
*/
public DynamicAppInfo() { }

Expand Down Expand Up @@ -79,6 +80,9 @@ public DynamicAppInfo[] newArray(int size) {
}
};

/**
* De-parcel {@link DynamicAppInfo} object.
*/
public DynamicAppInfo(Parcel in) {
this.applicationInfo = in.readParcelable(ApplicationInfo.class.getClassLoader());
this.topActivity = in.readParcelable(ComponentName.class.getClassLoader());
Expand All @@ -87,58 +91,58 @@ public DynamicAppInfo(Parcel in) {
}

/**
* Getter fot {@link #applicationInfo}.
* Getter for {@link #applicationInfo}.
*/
public ApplicationInfo getApplicationInfo() {
public @Nullable ApplicationInfo getApplicationInfo() {
return applicationInfo;
}

/**
* Setter fot {@link #applicationInfo}.
* Setter for {@link #applicationInfo}.
*/
public void setApplicationInfo(ApplicationInfo applicationInfo) {
public void setApplicationInfo(@Nullable ApplicationInfo applicationInfo) {
this.applicationInfo = applicationInfo;
}

/**
* Getter fot {@link #topActivity}.
* Getter for {@link #topActivity}.
*/
public ComponentName getTopActivity() {
public @Nullable ComponentName getTopActivity() {
return topActivity;
}

/**
* Setter fot {@link #topActivity}.
* Setter for {@link #topActivity}.
*/
public void setTopActivity(ComponentName topActivity) {
public void setTopActivity(@Nullable ComponentName topActivity) {
this.topActivity = topActivity;
}

/**
* Getter fot {@link #packageName}.
* Getter for {@link #packageName}.
*/
public String getPackageName() {
public @Nullable String getPackageName() {
return packageName;
}

/**
* Setter fot {@link #packageName}.
* Setter for {@link #packageName}.
*/
public void setPackageName(String packageName) {
public void setPackageName(@Nullable String packageName) {
this.packageName = packageName;
}

/**
* Getter fot {@link #label}.
* Getter for {@link #label}.
*/
public String getLabel() {
public @Nullable String getLabel() {
return label;
}

/**
* Setter fot {@link #label}.
* Setter for {@link #label}.
*/
public void setLabel(String label) {
public void setLabel(@Nullable String label) {
this.label = label;
}

Expand All @@ -150,6 +154,7 @@ public void setLabel(String label) {
* @return {@code true} if the two DynamicAppInfo are equal.
*/
public boolean equals(@NonNull DynamicAppInfo dynamicAppInfo) {
return getPackageName().equals(dynamicAppInfo.getPackageName());
return !(getPackageName() != null && dynamicAppInfo.getPackageName() != null)
|| getPackageName().equals(dynamicAppInfo.getPackageName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ public void onReceive(@NonNull final Context context, @Nullable Intent intent) {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ public void initializeEvents() {
/**
* Getter for {@link #mDynamicEventListener}.
*/
public DynamicEventListener getSpecialEventListener() {
public @NonNull DynamicEventListener getSpecialEventListener() {
return mDynamicEventListener;
}

/**
* Getter for {@link #mDynamicAppMonitor}.
*/
public DynamicAppMonitor getAppMonitor() {
public @NonNull DynamicAppMonitor getAppMonitor() {
return mDynamicAppMonitor;
}

Expand Down Expand Up @@ -188,9 +188,7 @@ public void onDestroy() {
unregisterReceiver(mSpecialEventReceiver);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mSpecialEventReceiver);
setAppMonitorTask(false);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception ignored) { }
}

/**
Expand Down Expand Up @@ -292,7 +290,7 @@ public void setDocked(boolean docked) {
* @see DynamicEngineUtils#ACTION_ON_CALL
* @see DynamicEngineUtils#ACTION_CALL_IDLE
*/
private class SpecialEventReceiver extends BroadcastReceiver {
class SpecialEventReceiver extends BroadcastReceiver {

@Override
public void onReceive(@NonNull Context context, @Nullable Intent intent) {
Expand Down Expand Up @@ -343,7 +341,7 @@ public void onReceive(@NonNull Context context, @Nullable Intent intent) {
/**
* Get a list of current ongoing events.
*/
protected ArrayList<String> getCurrentEvents() {
protected @NonNull ArrayList<String> getCurrentEvents() {
ArrayList<String> currentEvents = new ArrayList<>();
ArrayList<String> eventsPriority = DynamicPriority.getEventsPriority(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class DynamicAppMonitor extends AsyncTask<Void, DynamicAppInfo, Void> {
private boolean mRunning;

/**
* DynamicAppInfo for the foreground package.
* Dynamic app info for the foreground package.
*/
private DynamicAppInfo mDynamicAppInfo;

Expand Down Expand Up @@ -131,9 +131,7 @@ protected Void doInBackground(Void... params) {
}

Thread.sleep(ADE_THREAD_SLEEP_INTERVAL);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception ignored) { }
}

return null;
Expand Down Expand Up @@ -165,14 +163,14 @@ public boolean isRunning() {
/**
* Getter for {@link #mDynamicAppInfo}.
*/
public DynamicAppInfo getCurrentAppInfo() {
public @Nullable DynamicAppInfo getCurrentAppInfo() {
return mDynamicAppInfo;
}

/**
* Setter for {@link #mDynamicAppInfo}.
*/
private void setCurrentAppInfo(@Nullable DynamicAppInfo dynamicAppInfo) {
public void setCurrentAppInfo(@Nullable DynamicAppInfo dynamicAppInfo) {
this.mDynamicAppInfo = dynamicAppInfo;
}

Expand All @@ -184,9 +182,9 @@ public void setRunning(boolean running) {
}

/**
* Get DynamicAppInfo from the foreground package name.
* Get dynamic app info from the foreground package name.
*/
private DynamicAppInfo getForegroundAppInfo() {
private @Nullable DynamicAppInfo getForegroundAppInfo() {
String packageName = null;
DynamicAppInfo dynamicAppInfo = null;

Expand All @@ -213,7 +211,7 @@ private DynamicAppInfo getForegroundAppInfo() {
/**
* Get foreground package name on Android L and above devices.
*/
private String getForegroundPackage(long time, long interval) {
private @Nullable String getForegroundPackage(long time, long interval) {
String packageName = null;

UsageEvents usageEvents = mUsageStatsManager.queryEvents(time - interval * 1000, time);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.pranavpandey.android.dynamic.engine.model.DynamicAppInfo;
import com.pranavpandey.android.dynamic.engine.service.DynamicEngine;
Expand Down Expand Up @@ -55,21 +56,21 @@ public class DynamicEngineUtils {
* @param context Context to get {@link PackageManager}.
* @param packageName Package name to build the {@link DynamicAppInfo}.
*/
public static DynamicAppInfo getAppInfoFromPackage(
@NonNull Context context, String packageName) {
public static @Nullable DynamicAppInfo getAppInfoFromPackage(
@NonNull Context context, @Nullable String packageName) {
if (packageName != null) {
DynamicAppInfo dynamicAppInfo = new DynamicAppInfo();
try {
dynamicAppInfo.setApplicationInfo(
context.getPackageManager().getApplicationInfo(
packageName, PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
packageName, PackageManager.GET_META_DATA));

dynamicAppInfo.setPackageName(packageName);
dynamicAppInfo.setLabel(dynamicAppInfo.getApplicationInfo().
loadLabel(context.getPackageManager()).toString());
dynamicAppInfo.setPackageName(packageName);
if (dynamicAppInfo.getApplicationInfo() != null) {
dynamicAppInfo.setLabel(dynamicAppInfo.getApplicationInfo().
loadLabel(context.getPackageManager()).toString());
}
} catch (PackageManager.NameNotFoundException ignored) { }

return dynamicAppInfo;
}
Expand All @@ -83,7 +84,7 @@ public static DynamicAppInfo getAppInfoFromPackage(
*
* @see DynamicEngine
*/
public static IntentFilter getEventsIntentFilter() {
public static @NonNull IntentFilter getEventsIntentFilter() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
Expand All @@ -102,7 +103,7 @@ public static IntentFilter getEventsIntentFilter() {
*
* @see DynamicEngine
*/
public static IntentFilter getCallIntentFilter() {
public static @NonNull IntentFilter getCallIntentFilter() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_ON_CALL);
intentFilter.addAction(ACTION_CALL_IDLE);
Expand All @@ -114,7 +115,7 @@ public static IntentFilter getCallIntentFilter() {
* Get intent filter to register a broadcast receiver which can
* listen package added or removed broadcasts.
*/
public static IntentFilter getPackageIntentFilter() {
public static @NonNull IntentFilter getPackageIntentFilter() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
Expand Down

0 comments on commit 045567f

Please sign in to comment.