Commit 21ab40e2 authored by Grigory Fedorov's avatar Grigory Fedorov

Refactoring: AccountPainter used in BarPainter and StatusBarPainter.

parent 6eb9e0ca
...@@ -38,7 +38,6 @@ import com.xabber.android.data.notification.NotificationManager; ...@@ -38,7 +38,6 @@ import com.xabber.android.data.notification.NotificationManager;
import com.xabber.android.data.roster.OnContactChangedListener; import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.ui.adapter.ChatScrollIndicatorAdapter; import com.xabber.android.ui.adapter.ChatScrollIndicatorAdapter;
import com.xabber.android.ui.adapter.ChatViewerAdapter; import com.xabber.android.ui.adapter.ChatViewerAdapter;
import com.xabber.android.ui.helper.BarPainter;
import com.xabber.android.ui.helper.ManagedActivity; import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.android.ui.helper.StatusBarPainter; import com.xabber.android.ui.helper.StatusBarPainter;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
...@@ -384,13 +383,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -384,13 +383,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
} }
private void updateStatusBar() { private void updateStatusBar() {
String account;
if (isRecentChatsSelected) { if (isRecentChatsSelected) {
account = BarPainter.getFirstAccount(); statusBarPainter.updateWithDefaultColor();
} else { } else {
account = selectedChat.getAccount(); statusBarPainter.updateWithAccountName(selectedChat.getAccount());
} }
statusBarPainter.updateWithAccountName(account);
} }
private void updateRegisteredChats() { private void updateRegisteredChats() {
......
...@@ -7,7 +7,6 @@ import android.content.ClipboardManager; ...@@ -7,7 +7,6 @@ import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.NavUtils; import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
...@@ -50,7 +49,7 @@ import com.xabber.android.data.roster.AbstractContact; ...@@ -50,7 +49,7 @@ import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.RosterManager; import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.adapter.ChatMessageAdapter; import com.xabber.android.ui.adapter.ChatMessageAdapter;
import com.xabber.android.ui.dialog.ChatExportDialogFragment; import com.xabber.android.ui.dialog.ChatExportDialogFragment;
import com.xabber.android.ui.helper.BarPainter; import com.xabber.android.ui.helper.AccountPainter;
import com.xabber.android.ui.helper.ContactTitleInflater; import com.xabber.android.ui.helper.ContactTitleInflater;
import com.xabber.android.ui.preferences.ChatEditor; import com.xabber.android.ui.preferences.ChatEditor;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
...@@ -133,8 +132,8 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -133,8 +132,8 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
} }
}); });
BarPainter barPainter = new BarPainter((AppCompatActivity) getActivity(), toolbar); AccountPainter accountPainter = new AccountPainter(getActivity());
toolbar.setBackgroundColor(barPainter.getAccountColor(account)); toolbar.setBackgroundColor(accountPainter.getAccountMainColor(account));
sendButton = (ImageButton) view.findViewById(R.id.button_send_message); sendButton = (ImageButton) view.findViewById(R.id.button_send_message);
sendButton.setImageResource(R.drawable.ic_button_send_inactive_24dp); sendButton.setImageResource(R.drawable.ic_button_send_inactive_24dp);
......
...@@ -30,6 +30,7 @@ import com.xabber.android.data.roster.AbstractContact; ...@@ -30,6 +30,7 @@ import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.PresenceManager; import com.xabber.android.data.roster.PresenceManager;
import com.xabber.android.data.roster.RosterManager; import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.data.roster.SubscriptionRequest; import com.xabber.android.data.roster.SubscriptionRequest;
import com.xabber.android.ui.helper.AccountPainter;
import com.xabber.android.ui.helper.BarPainter; import com.xabber.android.ui.helper.BarPainter;
import com.xabber.android.ui.helper.SingleActivity; import com.xabber.android.ui.helper.SingleActivity;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
...@@ -77,10 +78,11 @@ public class ContactSubscription extends SingleActivity implements View.OnClickL ...@@ -77,10 +78,11 @@ public class ContactSubscription extends SingleActivity implements View.OnClickL
BarPainter barPainter = new BarPainter(this, toolbar); BarPainter barPainter = new BarPainter(this, toolbar);
barPainter.updateWithAccountName(account); barPainter.updateWithAccountName(account);
AccountPainter accountPainter = new AccountPainter(this);
View fakeToolbar = findViewById(R.id.fake_toolbar); View fakeToolbar = findViewById(R.id.fake_toolbar);
fakeToolbar.setBackgroundColor(barPainter.getAccountColor(account)); fakeToolbar.setBackgroundColor(accountPainter.getAccountMainColor(account));
toolbar.setBackgroundResource(android.R.color.transparent); toolbar.setBackgroundResource(android.R.color.transparent);
AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user); AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
...@@ -90,7 +92,7 @@ public class ContactSubscription extends SingleActivity implements View.OnClickL ...@@ -90,7 +92,7 @@ public class ContactSubscription extends SingleActivity implements View.OnClickL
((TextView)fakeToolbar.findViewById(R.id.dialog_message)).setText(subscriptionRequest.getConfirmation()); ((TextView)fakeToolbar.findViewById(R.id.dialog_message)).setText(subscriptionRequest.getConfirmation());
Button acceptButton = (Button) findViewById(R.id.accept_button); Button acceptButton = (Button) findViewById(R.id.accept_button);
acceptButton.setTextColor(barPainter.getAccountColor(account)); acceptButton.setTextColor(accountPainter.getAccountMainColor(account));
acceptButton.setOnClickListener(this); acceptButton.setOnClickListener(this);
findViewById(R.id.decline_button).setOnClickListener(this); findViewById(R.id.decline_button).setOnClickListener(this);
......
...@@ -4,7 +4,6 @@ import android.app.Activity; ...@@ -4,7 +4,6 @@ import android.app.Activity;
import android.app.ListFragment; import android.app.ListFragment;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.NavUtils; import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
...@@ -15,7 +14,7 @@ import android.widget.Toast; ...@@ -15,7 +14,7 @@ import android.widget.Toast;
import com.xabber.android.data.message.AbstractChat; import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.ui.adapter.ChatListAdapter; import com.xabber.android.ui.adapter.ChatListAdapter;
import com.xabber.android.ui.helper.BarPainter; import com.xabber.android.ui.helper.AccountPainter;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -81,8 +80,8 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt ...@@ -81,8 +80,8 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
toolbar.inflateMenu(R.menu.recent_chats); toolbar.inflateMenu(R.menu.recent_chats);
toolbar.setOnMenuItemClickListener(this); toolbar.setOnMenuItemClickListener(this);
BarPainter barPainter = new BarPainter((AppCompatActivity) getActivity(), toolbar); AccountPainter accountPainter = new AccountPainter(getActivity());
toolbar.setBackgroundColor(barPainter.getDefaultColor()); toolbar.setBackgroundColor(accountPainter.getDefaultMainColor());
return rootView; return rootView;
} }
......
...@@ -13,6 +13,7 @@ import java.util.List; ...@@ -13,6 +13,7 @@ import java.util.List;
public class AccountPainter { public class AccountPainter {
private final int themeMainColor; private final int themeMainColor;
private final int themeDarkColor; private final int themeDarkColor;
private final String[] accountColorNames;
private int[] accountMainColors; private int[] accountMainColors;
private int[] accountDarkColors; private int[] accountDarkColors;
...@@ -22,6 +23,8 @@ public class AccountPainter { ...@@ -22,6 +23,8 @@ public class AccountPainter {
accountMainColors = context.getResources().getIntArray(R.array.account_action_bar); accountMainColors = context.getResources().getIntArray(R.array.account_action_bar);
accountDarkColors = context.getResources().getIntArray(R.array.account_status_bar); accountDarkColors = context.getResources().getIntArray(R.array.account_status_bar);
accountColorNames = context.getResources().getStringArray(R.array.account_color_names);
themeMainColor = getThemeMainColor(context); themeMainColor = getThemeMainColor(context);
themeDarkColor = getThemeDarkColor(context); themeDarkColor = getThemeDarkColor(context);
} }
...@@ -76,4 +79,22 @@ public class AccountPainter { ...@@ -76,4 +79,22 @@ public class AccountPainter {
return getAccountDarkColor(firstAccount); return getAccountDarkColor(firstAccount);
} }
} }
public int getAccountMainColorByColorName(String targetColorName) {
return accountMainColors[getColorIndexByName(targetColorName)];
}
public int getAccountDarkColorByColorName(String targetColorName) {
return accountDarkColors[getColorIndexByName(targetColorName)];
}
private Integer getColorIndexByName(String targetColorName) {
for (int i = 0; i < accountColorNames.length; i++) {
String accountColorName = accountColorNames[i];
if (accountColorName.equals(targetColorName)) {
return i;
}
}
return null;
}
} }
package com.xabber.android.ui.helper; package com.xabber.android.ui.helper;
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import com.xabber.android.data.account.AccountManager;
import com.xabber.androiddev.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class BarPainter { public class BarPainter {
private final Toolbar toolbar; private final Toolbar toolbar;
private final int defaultSystemColor;
private int[] accountActionBarColors;
private String[] accountColorNames;
private StatusBarPainter statusBarPainter; private StatusBarPainter statusBarPainter;
private AccountPainter accountPainter;
public BarPainter(AppCompatActivity activity, Toolbar toolbar) { public BarPainter(AppCompatActivity activity, Toolbar toolbar) {
this.toolbar = toolbar; this.toolbar = toolbar;
statusBarPainter = new StatusBarPainter(activity); statusBarPainter = new StatusBarPainter(activity);
accountActionBarColors = activity.getResources().getIntArray(R.array.account_action_bar);
accountColorNames = activity.getResources().getStringArray(R.array.account_color_names);
TypedArray a = activity.getTheme().obtainStyledAttributes(R.style.Theme, new int[]{R.attr.colorPrimary}); accountPainter = new AccountPainter(activity);
int attributeResourceId = a.getResourceId(0, 0);
a.recycle();
defaultSystemColor = activity.getResources().getColor(attributeResourceId);
}
public static String getFirstAccount() {
List<String> list = new ArrayList<>();
list.addAll(AccountManager.getInstance().getAccounts());
Collections.sort(list);
if (list.isEmpty()) {
return null;
} else {
return list.get(0);
}
} }
public void updateWithAccountName(String account) { public void updateWithAccountName(String account) {
updateWithColorLevel(AccountManager.getInstance().getColorLevel(account)); toolbar.setBackgroundColor(accountPainter.getAccountMainColor(account));
} statusBarPainter.updateWithAccountName(account);
public void updateWithColorLevel(int colorLevel) {
toolbar.setBackgroundColor(accountActionBarColors[colorLevel]);
statusBarPainter.updateWithColorLevel(colorLevel);
} }
public void setDefaultColor() { public void setDefaultColor() {
String firstAccount = getFirstAccount(); toolbar.setBackgroundColor(accountPainter.getDefaultMainColor());
if (firstAccount == null) { statusBarPainter.updateWithDefaultColor();
toolbar.setBackgroundColor(defaultSystemColor);
statusBarPainter.restore();
} else {
updateWithAccountName(firstAccount);
}
} }
public void updateWithColorName(String targetColorName) { public void updateWithColorName(String targetColorName) {
for (int i = 0; i < accountColorNames.length; i++) { toolbar.setBackgroundColor(accountPainter.getAccountMainColorByColorName(targetColorName));
String accountColorName = accountColorNames[i]; statusBarPainter.updateWithColor(accountPainter.getAccountDarkColorByColorName(targetColorName));
if (accountColorName.equals(targetColorName)) {
updateWithColorLevel(i);
}
}
}
public int getAccountColor(String account) {
return accountActionBarColors[AccountManager.getInstance().getColorLevel(account)];
}
public int getDefaultColor() {
String firstAccount = getFirstAccount();
if (firstAccount == null) {
return defaultSystemColor;
} else {
return accountActionBarColors[AccountManager.getInstance().getColorLevel(firstAccount)];
}
} }
} }
...@@ -5,42 +5,34 @@ import android.support.v4.app.FragmentActivity; ...@@ -5,42 +5,34 @@ import android.support.v4.app.FragmentActivity;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import com.xabber.android.data.account.AccountManager;
import com.xabber.androiddev.R;
public class StatusBarPainter { public class StatusBarPainter {
private final AccountPainter accountPainter;
private Window window; private Window window;
private int defaultStatusBarColor;
private int[] accountStatusBarColors;
public StatusBarPainter(FragmentActivity activity) { public StatusBarPainter(FragmentActivity activity) {
accountStatusBarColors = activity.getResources().getIntArray(R.array.account_status_bar); accountPainter = new AccountPainter(activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window = activity.getWindow(); window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
defaultStatusBarColor = window.getStatusBarColor();
} }
} }
public void updateWithAccountName(String account) { public void updateWithAccountName(String account) {
updateWithColorLevel(AccountManager.getInstance().getColorLevel(account)); updateWithColor(accountPainter.getAccountDarkColor(account));
} }
public void updateWithColorLevel(int colorLevel) {
public void updateWithColor(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(accountStatusBarColors[colorLevel]); window.setStatusBarColor(color);
} }
} }
public void restore() { public void updateWithDefaultColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { updateWithColor(accountPainter.getDefaultDarkColor());
window.setStatusBarColor(defaultStatusBarColor);
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment