diff --git a/res/layout/drawer.xml b/res/layout/drawer.xml index 9360db0ec52..f024408a8c7 100644 --- a/res/layout/drawer.xml +++ b/res/layout/drawer.xml @@ -28,33 +28,10 @@ android:background="@color/background_color" > - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + Accounts Alle Dateien diff --git a/res/values/drawer_resources.xml b/res/values/drawer_resources.xml index 287eb6982e9..01f2a1a6d45 100644 --- a/res/values/drawer_resources.xml +++ b/res/values/drawer_resources.xml @@ -20,20 +20,18 @@ - - @string/drawer_item_all_files + @string/prefs_accounts @string/actionbar_settings @string/actionbar_logger - - @string/drawer_item_all_files + @string/drawer_item_accounts @string/drawer_item_settings @string/drawer_item_logs diff --git a/res/values/strings.xml b/res/values/strings.xml index 91689925b6b..4fedd3eae8d 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -20,8 +20,7 @@ - - + Accounts All files diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 992595b20b5..cbf17ae21d6 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -45,6 +45,7 @@ import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; +import android.widget.ImageView; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; @@ -86,6 +87,7 @@ import com.owncloud.android.ui.adapter.NavigationDrawerListAdapter; import com.owncloud.android.ui.dialog.LoadingDialog; import com.owncloud.android.ui.dialog.SharePasswordDialogFragment; +import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.ErrorMessageAdapter; import java.util.ArrayList; @@ -168,10 +170,7 @@ public class FileActivity extends AppCompatActivity protected NavigationDrawerListAdapter mNavigationDrawerAdapter = null; - - - // TODO re-enable when "Accounts" is available in Navigation Drawer -// protected boolean mShowAccounts = false; + protected boolean mShowAccounts = false; /** * Loads the ownCloud {@link Account} and main {@link OCFile} to be handled by the instance of @@ -359,20 +358,19 @@ protected void initDrawer(){ RelativeLayout navigationDrawerLayout = (RelativeLayout) findViewById(R.id.left_drawer); mDrawerList = (ListView) navigationDrawerLayout.findViewById(R.id.drawer_list); - // TODO re-enable when "Accounts" is available in Navigation Drawer -// // load Account in the Drawer Title -// // User-Icon -// ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon); -// userIcon.setImageResource(DisplayUtils.getSeasonalIconId()); -// -// // Username -// TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username); -// Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()); -// -// if (account != null) { -// int lastAtPos = account.name.lastIndexOf("@"); -// username.setText(account.name.substring(0, lastAtPos)); -// } + // load Account in the Drawer Title + // User-Icon + ImageView userIcon = (ImageView) navigationDrawerLayout.findViewById(R.id.drawer_userIcon); + userIcon.setImageResource(DisplayUtils.getSeasonalIconId()); + + // Username + TextView username = (TextView) navigationDrawerLayout.findViewById(R.id.drawer_username); + Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext()); + + if (account != null) { + int lastAtPos = account.name.lastIndexOf("@"); + username.setText(account.name.substring(0, lastAtPos)); + } // Display username in drawer setUsernameInDrawer(navigationDrawerLayout, AccountUtils.getCurrentOwnCloudAccount(getApplicationContext())); @@ -387,26 +385,26 @@ protected void initDrawer(){ // nav drawer items mDrawerItems = new ArrayList(); // adding nav drawer items to array - // TODO re-enable when "Accounts" is available in Navigation Drawer - // Accounts - // mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0], - // mDrawerContentDescriptions[0])); // All Files mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[0], mDrawerContentDescriptions[0], R.drawable.ic_folder_open)); + // Accounts + mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[1], mDrawerContentDescriptions[1], + R.drawable.ic_account_circle)); + // TODO Enable when "On Device" is recovered // On Device //mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2], // mDrawerContentDescriptions[2])); // Settings - mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[1], mDrawerContentDescriptions[1], + mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2], mDrawerContentDescriptions[2], R.drawable.ic_settings)); // Logs if (BuildConfig.DEBUG) { - mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[2], - mDrawerContentDescriptions[2],R.drawable.ic_log)); + mDrawerItems.add(new NavigationDrawerItem(mDrawerTitles[3], + mDrawerContentDescriptions[3],R.drawable.ic_log)); } // setting the nav drawer list adapter @@ -997,10 +995,9 @@ public void restart(){ startActivity(i); } -// TODO re-enable when "Accounts" is available in Navigation Drawer -// public void closeDrawer() { -// mDrawerLayout.closeDrawers(); -// } + public void closeDrawer() { + mDrawerLayout.closeDrawers(); + } public void allFilesOption(){ restart(); @@ -1009,18 +1006,10 @@ public void allFilesOption(){ private class DrawerItemClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - // TODO re-enable when "Accounts" is available in Navigation Drawer -// if (mShowAccounts && position > 0){ -// position = position - 1; -// } - switch (position){ - // TODO re-enable when "Accounts" is available in Navigation Drawer -// case 0: // Accounts -// mShowAccounts = !mShowAccounts; -// mNavigationDrawerAdapter.setShowAccounts(mShowAccounts); -// mNavigationDrawerAdapter.notifyDataSetChanged(); -// break; - + if (mShowAccounts && position > 0){ + position = position - 1; + } + switch (position) { case 0: // All Files allFilesOption(); mDrawerLayout.closeDrawers(); @@ -1032,19 +1021,25 @@ public void onItemClick(AdapterView parent, View view, int position, long id) // mDrawerLayout.closeDrawers(); // break; - case 1: // Settings - Intent settingsIntent = new Intent(getApplicationContext(), - Preferences.class); - startActivity(settingsIntent); - mDrawerLayout.closeDrawers(); + case 1: // Accounts + mShowAccounts = !mShowAccounts; + mNavigationDrawerAdapter.setShowAccounts(mShowAccounts); + mNavigationDrawerAdapter.notifyDataSetChanged(); break; - case 2: // Logs - Intent loggerIntent = new Intent(getApplicationContext(), + case 2: // Settings + Intent settingsIntent = new Intent(getApplicationContext(), + Preferences.class); + startActivity(settingsIntent); + mDrawerLayout.closeDrawers(); + break; + + case 3: // Logs + Intent loggerIntent = new Intent(getApplicationContext(), LogHistoryActivity.class); - startActivity(loggerIntent); - mDrawerLayout.closeDrawers(); - break; + startActivity(loggerIntent); + mDrawerLayout.closeDrawers(); + break; } } } diff --git a/src/com/owncloud/android/ui/adapter/NavigationDrawerListAdapter.java b/src/com/owncloud/android/ui/adapter/NavigationDrawerListAdapter.java index a8a60274b25..c606d231ca5 100644 --- a/src/com/owncloud/android/ui/adapter/NavigationDrawerListAdapter.java +++ b/src/com/owncloud/android/ui/adapter/NavigationDrawerListAdapter.java @@ -24,21 +24,31 @@ import android.accounts.Account; import android.accounts.AccountManager; import android.content.Context; +import android.graphics.Color; +import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; +import android.widget.RadioButton; +import android.widget.RadioGroup; import android.widget.TextView; import com.owncloud.android.MainApp; import com.owncloud.android.R; import com.owncloud.android.authentication.AccountUtils; +import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.ui.NavigationDrawerItem; +import com.owncloud.android.ui.TextDrawable; import com.owncloud.android.ui.activity.FileActivity; +import com.owncloud.android.utils.BitmapUtils; +import java.math.BigInteger; +import java.security.MessageDigest; import java.util.ArrayList; +import java.util.Locale; public class NavigationDrawerListAdapter extends BaseAdapter { @@ -117,93 +127,91 @@ public View getView(int position, View convertView, ViewGroup parent) { return view; } - // TODO re-enable when "Accounts" is available in Navigation Drawer // Account -// if (mAll.get(position) instanceof Account[]){ -// final View view = inflator.inflate(R.layout.drawer_account_group, null); -// -// final RadioGroup group = (RadioGroup) view.findViewById(R.id.drawer_radio_group); -// -// for (Account account : mAccounts) { -// RadioButton rb = new RadioButton(mContext); -// -// rb.setText(account.name); -// rb.setContentDescription(account.name); -// rb.setTextColor(Color.BLACK); -// rb.setEllipsize(TextUtils.TruncateAt.MIDDLE); -// rb.setSingleLine(); -// rb.setCompoundDrawablePadding(30); -// -// -// try { -// // using adapted algorithm from /core/js/placeholder.js:50 -// int lastAtPos = account.name.lastIndexOf("@"); -// String username = account.name.substring(0, lastAtPos); -// byte[] seed = username.getBytes("UTF-8"); -// MessageDigest md = MessageDigest.getInstance("MD5"); -//// Integer seedMd5Int = Math.abs(new String(Hex.encodeHex(seedMd5)) -//// .hashCode()); -// Integer seedMd5Int = String.format(Locale.ROOT, "%032x", -// new BigInteger(1, md.digest(seed))).hashCode(); -// -// double maxRange = java.lang.Integer.MAX_VALUE; -// float hue = (float) (seedMd5Int / maxRange * 360); -// -// int[] rgb = BitmapUtils.HSLtoRGB(hue, 90.0f, 65.0f, 1.0f); -// -// TextDrawable text = new TextDrawable(username.substring(0, 1).toUpperCase(), -// rgb[0], rgb[1], rgb[2]); -// rb.setCompoundDrawablesWithIntrinsicBounds(text, null, null, null); -// -// -// } catch (Exception e){ -// Log_OC.d(TAG, e.toString()); -// rb.setTextColor(mContext.getResources().getColor(R.color.black)); -// } -// RadioGroup.LayoutParams params = new RadioGroup.LayoutParams( -// LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); -// params.weight=1.0f; -// params.setMargins(15, 5, 5, 5); -// -// // Check the current account that is being used -// if (account.name.equals(mCurrentAccount.name)) { -// rb.setChecked(true); -// } else { -// rb.setChecked(false); -// } -// -// group.addView(rb, params); -// } -// -// group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ -// public void onCheckedChanged(RadioGroup group, int checkedId) { -// // checkedId is the RadioButton selected -// RadioButton rb = (RadioButton) view.findViewById(checkedId); -// -// AccountUtils.setCurrentOwnCloudAccount(mContext,rb.getText().toString()); -// notifyDataSetChanged(); -// mFileActivity.closeDrawer(); -// -// // restart the main activity -// mFileActivity.restart(); -// } -// }); -// -// return view; -// } + if (mAll.get(position) instanceof Account[]){ + final View view = inflator.inflate(R.layout.drawer_account_group, null); + + final RadioGroup group = (RadioGroup) view.findViewById(R.id.drawer_radio_group); + + for (Account account : mAccounts) { + RadioButton rb = new RadioButton(mContext); + + rb.setText(account.name); + rb.setContentDescription(account.name); + rb.setTextColor(Color.BLACK); + rb.setEllipsize(TextUtils.TruncateAt.MIDDLE); + rb.setSingleLine(); + rb.setCompoundDrawablePadding(30); + + + try { + // using adapted algorithm from /core/js/placeholder.js:50 + int lastAtPos = account.name.lastIndexOf("@"); + String username = account.name.substring(0, lastAtPos); + byte[] seed = username.getBytes("UTF-8"); + MessageDigest md = MessageDigest.getInstance("MD5"); +// Integer seedMd5Int = Math.abs(new String(Hex.encodeHex(seedMd5)) +// .hashCode()); + Integer seedMd5Int = String.format(Locale.ROOT, "%032x", + new BigInteger(1, md.digest(seed))).hashCode(); + + double maxRange = java.lang.Integer.MAX_VALUE; + float hue = (float) (seedMd5Int / maxRange * 360); + + int[] rgb = BitmapUtils.HSLtoRGB(hue, 90.0f, 65.0f, 1.0f); + + TextDrawable text = new TextDrawable(username.substring(0, 1).toUpperCase(), + rgb[0], rgb[1], rgb[2]); + rb.setCompoundDrawablesWithIntrinsicBounds(text, null, null, null); + + + } catch (Exception e){ + Log_OC.d(TAG, e.toString()); + rb.setTextColor(mContext.getResources().getColor(R.color.black)); + } + RadioGroup.LayoutParams params = new RadioGroup.LayoutParams( + RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT); + params.weight=1.0f; + params.setMargins(15, 5, 5, 5); + + // Check the current account that is being used + if (account.name.equals(mCurrentAccount.name)) { + rb.setChecked(true); + } else { + rb.setChecked(false); + } + + group.addView(rb, params); + } + + group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ + public void onCheckedChanged(RadioGroup group, int checkedId) { + // checkedId is the RadioButton selected + RadioButton rb = (RadioButton) view.findViewById(checkedId); + + AccountUtils.setCurrentOwnCloudAccount(mContext,rb.getText().toString()); + notifyDataSetChanged(); + mFileActivity.closeNavDrawer(); + + // restart the main activity + mFileActivity.restart(); + } + }); + + return view; + } } return convertView; } - //TODO re-enable when "Accounts" is available in Navigation Drawer // TODO update Account List after creating a new account and on fresh installation -// public void setShowAccounts(boolean value){ -// mAll.clear(); -// mAll.addAll(mNavigationDrawerItems); -// -// if (value){ -// mAll.add(1, mAccounts); -// } -// mShowAccounts = value; -// } + public void setShowAccounts(boolean value){ + mAll.clear(); + mAll.addAll(mNavigationDrawerItems); + + if (value){ + mAll.add(1, mAccounts); + } + mShowAccounts = value; + } }