Skip to content

Commit

Permalink
Add Favorite
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersantos committed Jul 1, 2015
1 parent f0f2aaf commit 09a9453
Show file tree
Hide file tree
Showing 23 changed files with 162 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
Expand All @@ -31,16 +32,20 @@
import com.javiersantos.mlmanager.utils.UtilsDialog;
import com.javiersantos.mlmanager.utils.UtilsUI;

import java.util.Set;

public class AppActivity extends AppCompatActivity {
// Load Settings
private AppPreferences appPreferences;

// General variables
private AppInfo appInfo;
private Set<String> appFavorites;

// Configuration variables
private int UNINSTALL_REQUEST_CODE = 1;
private Context context;
private MenuItem item_favorite;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -91,7 +96,7 @@ private void setScreenElements() {
CardView start = (CardView) findViewById(R.id.start_card);
CardView extract = (CardView) findViewById(R.id.extract_card);
CardView uninstall = (CardView) findViewById(R.id.uninstall_card);
final CardView cache = (CardView) findViewById(R.id.cache_card);
CardView cache = (CardView) findViewById(R.id.cache_card);
CardView clearData = (CardView) findViewById(R.id.clear_data_card);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

Expand Down Expand Up @@ -213,6 +218,8 @@ private void getInitialConfiguration() {
Boolean appIsSystem = getIntent().getExtras().getBoolean("app_isSystem");

appInfo = new AppInfo(appName, appApk, appVersion, appSource, appData, appIcon, appIsSystem);
appFavorites = appPreferences.getFavoriteApps();

}

@Override
Expand All @@ -221,12 +228,35 @@ public void onBackPressed() {
overridePendingTransition(R.anim.fade_forward, R.anim.slide_out_right);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_app, menu);
return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
item_favorite = menu.findItem(R.id.action_favorite);
UtilsApp.setAppFavorite(context, item_favorite, UtilsApp.isAppFavorite(appInfo.getAPK(), appFavorites));
return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
finish();
return true;
case R.id.action_favorite:
if (UtilsApp.isAppFavorite(appInfo.getAPK(), appFavorites)) {
appFavorites.remove(appInfo.getAPK());
appPreferences.setFavoriteApps(appFavorites);
} else {
appFavorites.add(appInfo.getAPK());
appPreferences.setFavoriteApps(appFavorites);
}
UtilsApp.setAppFavorite(context, item_favorite, UtilsApp.isAppFavorite(appInfo.getAPK(), appFavorites));
return true;
}

return super.onOptionsItemSelected(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ public class MainActivity extends AppCompatActivity implements SearchView.OnQuer
private AppPreferences appPreferences;

// General variables
private List<AppInfo> appList = new ArrayList<>();
private List<AppInfo> appSystemList = new ArrayList<>();

private List<String> appListName = new ArrayList<>();
private List<String> appListAPK = new ArrayList<>();
private List<String> appListVersion = new ArrayList<>();
private List<String> appListSource = new ArrayList<>();
private List<String> appListData = new ArrayList<>();
private List<Drawable> appListIcon = new ArrayList<>();

private List<String> appSystemListName = new ArrayList<>();
private List<String> appSystemListAPK = new ArrayList<>();
private List<String> appSystemListVersion = new ArrayList<>();
Expand All @@ -61,6 +65,7 @@ public class MainActivity extends AppCompatActivity implements SearchView.OnQuer

private AppAdapter appAdapter;
private AppAdapter appSystemAdapter;
private AppAdapter appFavoriteAdapter;

// Configuration variables
private Boolean doubleBackToExitPressedOnce = false;
Expand Down Expand Up @@ -97,7 +102,7 @@ protected void onCreate(Bundle savedInstanceState) {
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);

setNavigationDrawer(appAdapter, appSystemAdapter, recyclerView);
setNavigationDrawer(appAdapter, appSystemAdapter, appFavoriteAdapter, recyclerView);

progressWheel.setBarColor(appPreferences.getPrimaryColorPref());
progressWheel.setVisibility(View.VISIBLE);
Expand All @@ -122,8 +127,8 @@ private void setInitialConfiguration() {
}
}

private void setNavigationDrawer(AppAdapter appAdapter, AppAdapter appSystemAdapter, RecyclerView recyclerView) {
drawer = UtilsUI.setNavigationDrawer(this, getApplicationContext(), toolbar, appAdapter, appSystemAdapter, recyclerView);
private void setNavigationDrawer(AppAdapter appAdapter, AppAdapter appSystemAdapter, AppAdapter appFavoriteAdapter, RecyclerView recyclerView) {
drawer = UtilsUI.setNavigationDrawer(this, getApplicationContext(), toolbar, appAdapter, appSystemAdapter, appFavoriteAdapter, recyclerView);
}

class getInstalledApps extends AsyncTask<Void, String, Void> {
Expand Down Expand Up @@ -180,6 +185,7 @@ public int compare(PackageInfo p1, PackageInfo p2) {
});
break;
}

for (PackageInfo packageInfo : packages) {
if (!(packageManager.getApplicationLabel(packageInfo.applicationInfo).equals("") || packageInfo.packageName.equals(""))) {
if (packageManager.getLaunchIntentForPackage(packageInfo.packageName) != null) {
Expand Down Expand Up @@ -230,15 +236,19 @@ protected void onProgressUpdate(String... progress) {
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
appAdapter = new AppAdapter(createList(appListName, appListAPK, appListVersion, appListSource, appListData, appListIcon, false), context);
appSystemAdapter = new AppAdapter(createList(appSystemListName, appSystemListAPK, appSystemListVersion, appSystemListSource, appSystemListData, appSystemListIcon, true), context);

appList = createList(appListName, appListAPK, appListVersion, appListSource, appListData, appListIcon, false);
appAdapter = new AppAdapter(appList, context);
appSystemList = createList(appSystemListName, appSystemListAPK, appSystemListVersion, appSystemListSource, appSystemListData, appSystemListIcon, true);
appSystemAdapter = new AppAdapter(appSystemList, context);
appFavoriteAdapter = new AppAdapter(getFavoriteList(appList, appSystemList), context);

fastScroller.setVisibility(View.VISIBLE);
recyclerView.setAdapter(appAdapter);
progressWheel.setVisibility(View.GONE);
searchItem.setVisible(true);

setNavigationDrawer(appAdapter, appSystemAdapter, recyclerView);
setNavigationDrawer(appAdapter, appSystemAdapter, appFavoriteAdapter, recyclerView);
}

}
Expand All @@ -251,15 +261,32 @@ private void setAppDir() {
}

private List<AppInfo> createList(List<String> apps, List<String> apks, List<String> versions, List<String> sources, List<String> data, List<Drawable> icons, Boolean isSystem) {
List<AppInfo> res = new ArrayList<AppInfo>();
for (int i=0; i < apps.size(); i++) {
List<AppInfo> res = new ArrayList<>();
for (int i = 0; i < apps.size(); i++) {
AppInfo appInfo = new AppInfo(apps.get(i), apks.get(i), versions.get(i), sources.get(i), data.get(i), icons.get(i), isSystem);
res.add(appInfo);
}

return res;
}

private List<AppInfo> getFavoriteList(List<AppInfo> appList, List<AppInfo> appSystemList) {
List<AppInfo> res = new ArrayList<>();

for (AppInfo app : appList) {
if (UtilsApp.isAppFavorite(app.getAPK(), appPreferences.getFavoriteApps())) {
res.add(app);
}
}
for (AppInfo app : appSystemList) {
if (UtilsApp.isAppFavorite(app.getAPK(), appPreferences.getFavoriteApps())) {
res.add(app);
}
}

return res;
}

@Override
public boolean onQueryTextChange(String search) {
if (search.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import com.javiersantos.mlmanager.R;

import java.util.HashSet;
import java.util.Set;

public class AppPreferences {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
Expand All @@ -19,6 +22,7 @@ public class AppPreferences {
public static final String KeyCustomFilename = "prefCustomFilename";
public static final String KeySortMode = "prefSortMode";
public static final String KeyIsRooted = "prefIsRooted";
public static final String KeyFavoriteApps = "prefFavoriteApps";

public AppPreferences(Context context) {
this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Expand Down Expand Up @@ -91,4 +95,15 @@ public void setSortMode(String res) {
editor.commit();
}

public Set<String> getFavoriteApps() {
return sharedPreferences.getStringSet(KeyFavoriteApps, new HashSet<String>());
}

public void setFavoriteApps(Set<String> favoriteApps) {
editor.remove(KeyFavoriteApps);
editor.commit();
editor.putStringSet(KeyFavoriteApps, favoriteApps);
editor.commit();
}

}
23 changes: 20 additions & 3 deletions app/src/main/java/com/javiersantos/mlmanager/utils/UtilsApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;

import com.javiersantos.mlmanager.AppInfo;
import com.javiersantos.mlmanager.MLManagerApplication;
import com.javiersantos.mlmanager.R;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Set;

public class UtilsApp {
// Load Settings
Expand All @@ -27,8 +29,6 @@ public static Boolean copyFile(AppInfo appInfo) {

File initialFile = new File(appInfo.getSource());
File finalFile = getOutputFilename(appInfo);
Log.i("Initial: ", initialFile.toString());
Log.i("Source: ", finalFile.toString());

try {
FileUtils.copyFile(initialFile, finalFile);
Expand Down Expand Up @@ -127,4 +127,21 @@ public static Intent getShareIntent(File file) {
return intent;
}

public static Boolean isAppFavorite(String apk, Set<String> appFavorites) {
Boolean res = false;
if (appFavorites.contains(apk)) {
res = true;
}

return res;
}

public static void setAppFavorite(Context context, MenuItem menuItem, Boolean isFavorite) {
if (isFavorite) {
menuItem.setIcon(context.getResources().getDrawable(R.drawable.ic_star_white));
} else {
menuItem.setIcon(context.getResources().getDrawable(R.drawable.ic_star_border_white));
}
}

}
16 changes: 13 additions & 3 deletions app/src/main/java/com/javiersantos/mlmanager/utils/UtilsUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public static int darker (int color, double factor) {
return Color.argb(a, Math.max((int) (r * factor), 0), Math.max((int) (g * factor), 0), Math.max((int) (b * factor), 0));
}

public static Drawer setNavigationDrawer (Activity activity, final Context context, Toolbar toolbar, final AppAdapter appAdapter, final AppAdapter appSystemAdapter, final RecyclerView recyclerView) {
public static Drawer setNavigationDrawer (Activity activity, final Context context, Toolbar toolbar, final AppAdapter appAdapter, final AppAdapter appSystemAdapter, final AppAdapter appFavoriteAdapter, final RecyclerView recyclerView) {
int header;
appPreferences = MLManagerApplication.getAppPreferences();
String apps, systemApps;
String apps, systemApps, favorites;

if (getDayOrNight() == 1) {
header = R.drawable.header_day;
Expand All @@ -60,6 +60,11 @@ public static Drawer setNavigationDrawer (Activity activity, final Context conte
} else {
systemApps = context.getResources().getString(R.string.loading);
}
if (appFavoriteAdapter != null) {
favorites = Integer.toString(appFavoriteAdapter.getItemCount());
} else {
favorites = context.getResources().getString(R.string.loading);
}

AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(activity)
Expand All @@ -75,6 +80,8 @@ public static Drawer setNavigationDrawer (Activity activity, final Context conte
new PrimaryDrawerItem().withName(context.getResources().getString(R.string.action_apps)).withIcon(FontAwesome.Icon.faw_mobile).withBadge(apps),
new PrimaryDrawerItem().withName(context.getResources().getString(R.string.action_system_apps)).withIcon(FontAwesome.Icon.faw_android).withBadge(systemApps),
new DividerDrawerItem(),
new PrimaryDrawerItem().withName(context.getResources().getString(R.string.action_favorites)).withIcon(FontAwesome.Icon.faw_star).withBadge(favorites),
new DividerDrawerItem(),
new SecondaryDrawerItem().withName(context.getResources().getString(R.string.action_settings)).withIcon(FontAwesome.Icon.faw_cog).withCheckable(false),
new SecondaryDrawerItem().withName(context.getResources().getString(R.string.action_about)).withIcon(FontAwesome.Icon.faw_info_circle).withCheckable(false)
)
Expand All @@ -89,9 +96,12 @@ public boolean onItemClick(AdapterView<?> parent, View view, int position, long
recyclerView.setAdapter(appSystemAdapter);
break;
case 3:
recyclerView.setAdapter(appFavoriteAdapter);
break;
case 5:
context.startActivity(new Intent(context, SettingsActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
break;
case 4:
case 6:
context.startActivity(new Intent(context, AboutActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
break;
default:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-hdpi/ic_star_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-mdpi/ic_star_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xhdpi/ic_star_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxhdpi/ic_star_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable-xxxhdpi/ic_star_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/fab_label_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/black_semi_transparent"/>
<padding
android:left="16dp"
android:top="4dp"
android:right="16dp"
android:bottom="4dp"/>
<corners
android:radius="2dp"/>
</shape>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_app.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
android:layout_height="match_parent">
<include
android:id="@+id/fab"
layout="@layout/fab" />
layout="@layout/fab_single" />
</FrameLayout>

</RelativeLayout>
26 changes: 26 additions & 0 deletions app/src/main/res/layout/fab_multiple.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<com.getbase.floatingactionbutton.FloatingActionsMenu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="@dimen/fab_margin"
app:fab_labelStyle="@style/menu_labels_style"
app:fab_addButtonColorNormal="@color/fab">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_colorNormal="@color/white"
app:fab_colorPressed="@color/white_pressed"
app:fab_title="@string/action_share" />
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_colorNormal="@color/white"
app:fab_colorPressed="@color/white_pressed"
app:fab_title="@string/action_favorites" />
</com.getbase.floatingactionbutton.FloatingActionsMenu>
File renamed without changes.
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_app.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".AppActivity">
<item android:id="@+id/action_share" android:title="@string/action_share" app:showAsAction="ifRoom" app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
<item android:id="@+id/action_favorite" android:title="@string/action_favorites" app:showAsAction="ifRoom" />
</menu>
4 changes: 3 additions & 1 deletion app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
<color name="text_primary">#212121</color>
<color name="bkg_card">#8C9EFF</color>
<color name="background">#E3E3E3</color>
<color name="white">#FFFFFF</color>
<color name="white">#FAFAFA</color>
<color name="white_pressed">#F1F1F1</color>
<color name="divider">#E0E0E0</color>
<color name="transparent">#00000000</color>
<color name="black_semi_transparent">#B2000000</color>

<color name="card_blue">#00BED4</color>
<color name="card_green">#8CC24A</color>
Expand Down
Loading

0 comments on commit 09a9453

Please sign in to comment.