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;
import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.ui.adapter.ChatScrollIndicatorAdapter;
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.StatusBarPainter;
import com.xabber.androiddev.R;
......@@ -384,13 +383,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
}
private void updateStatusBar() {
String account;
if (isRecentChatsSelected) {
account = BarPainter.getFirstAccount();
statusBarPainter.updateWithDefaultColor();
} else {
account = selectedChat.getAccount();
statusBarPainter.updateWithAccountName(selectedChat.getAccount());
}
statusBarPainter.updateWithAccountName(account);
}
private void updateRegisteredChats() {
......
......@@ -7,7 +7,6 @@ import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
......@@ -50,7 +49,7 @@ import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.adapter.ChatMessageAdapter;
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.preferences.ChatEditor;
import com.xabber.androiddev.R;
......@@ -133,8 +132,8 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
}
});
BarPainter barPainter = new BarPainter((AppCompatActivity) getActivity(), toolbar);
toolbar.setBackgroundColor(barPainter.getAccountColor(account));
AccountPainter accountPainter = new AccountPainter(getActivity());
toolbar.setBackgroundColor(accountPainter.getAccountMainColor(account));
sendButton = (ImageButton) view.findViewById(R.id.button_send_message);
sendButton.setImageResource(R.drawable.ic_button_send_inactive_24dp);
......
......@@ -30,6 +30,7 @@ import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.PresenceManager;
import com.xabber.android.data.roster.RosterManager;
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.SingleActivity;
import com.xabber.androiddev.R;
......@@ -77,10 +78,11 @@ public class ContactSubscription extends SingleActivity implements View.OnClickL
BarPainter barPainter = new BarPainter(this, toolbar);
barPainter.updateWithAccountName(account);
AccountPainter accountPainter = new AccountPainter(this);
View fakeToolbar = findViewById(R.id.fake_toolbar);
fakeToolbar.setBackgroundColor(barPainter.getAccountColor(account));
fakeToolbar.setBackgroundColor(accountPainter.getAccountMainColor(account));
toolbar.setBackgroundResource(android.R.color.transparent);
AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
......@@ -90,7 +92,7 @@ public class ContactSubscription extends SingleActivity implements View.OnClickL
((TextView)fakeToolbar.findViewById(R.id.dialog_message)).setText(subscriptionRequest.getConfirmation());
Button acceptButton = (Button) findViewById(R.id.accept_button);
acceptButton.setTextColor(barPainter.getAccountColor(account));
acceptButton.setTextColor(accountPainter.getAccountMainColor(account));
acceptButton.setOnClickListener(this);
findViewById(R.id.decline_button).setOnClickListener(this);
......
......@@ -4,7 +4,6 @@ import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
......@@ -15,7 +14,7 @@ import android.widget.Toast;
import com.xabber.android.data.message.AbstractChat;
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 java.util.ArrayList;
......@@ -81,8 +80,8 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
toolbar.inflateMenu(R.menu.recent_chats);
toolbar.setOnMenuItemClickListener(this);
BarPainter barPainter = new BarPainter((AppCompatActivity) getActivity(), toolbar);
toolbar.setBackgroundColor(barPainter.getDefaultColor());
AccountPainter accountPainter = new AccountPainter(getActivity());
toolbar.setBackgroundColor(accountPainter.getDefaultMainColor());
return rootView;
}
......
......@@ -13,6 +13,7 @@ import java.util.List;
public class AccountPainter {
private final int themeMainColor;
private final int themeDarkColor;
private final String[] accountColorNames;
private int[] accountMainColors;
private int[] accountDarkColors;
......@@ -22,6 +23,8 @@ public class AccountPainter {
accountMainColors = context.getResources().getIntArray(R.array.account_action_bar);
accountDarkColors = context.getResources().getIntArray(R.array.account_status_bar);
accountColorNames = context.getResources().getStringArray(R.array.account_color_names);
themeMainColor = getThemeMainColor(context);
themeDarkColor = getThemeDarkColor(context);
}
......@@ -76,4 +79,22 @@ public class AccountPainter {
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;
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatActivity;
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 {
private final Toolbar toolbar;
private final int defaultSystemColor;
private int[] accountActionBarColors;
private String[] accountColorNames;
private StatusBarPainter statusBarPainter;
private AccountPainter accountPainter;
public BarPainter(AppCompatActivity activity, Toolbar toolbar) {
this.toolbar = toolbar;
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});
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);
}
accountPainter = new AccountPainter(activity);
}
public void updateWithAccountName(String account) {
updateWithColorLevel(AccountManager.getInstance().getColorLevel(account));
}
public void updateWithColorLevel(int colorLevel) {
toolbar.setBackgroundColor(accountActionBarColors[colorLevel]);
statusBarPainter.updateWithColorLevel(colorLevel);
toolbar.setBackgroundColor(accountPainter.getAccountMainColor(account));
statusBarPainter.updateWithAccountName(account);
}
public void setDefaultColor() {
String firstAccount = getFirstAccount();
if (firstAccount == null) {
toolbar.setBackgroundColor(defaultSystemColor);
statusBarPainter.restore();
} else {
updateWithAccountName(firstAccount);
}
toolbar.setBackgroundColor(accountPainter.getDefaultMainColor());
statusBarPainter.updateWithDefaultColor();
}
public void updateWithColorName(String targetColorName) {
for (int i = 0; i < accountColorNames.length; i++) {
String accountColorName = accountColorNames[i];
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)];
}
toolbar.setBackgroundColor(accountPainter.getAccountMainColorByColorName(targetColorName));
statusBarPainter.updateWithColor(accountPainter.getAccountDarkColorByColorName(targetColorName));
}
}
......@@ -5,42 +5,34 @@ import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.view.WindowManager;
import com.xabber.android.data.account.AccountManager;
import com.xabber.androiddev.R;
public class StatusBarPainter {
private final AccountPainter accountPainter;
private Window window;
private int defaultStatusBarColor;
private int[] accountStatusBarColors;
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) {
window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
defaultStatusBarColor = window.getStatusBarColor();
}
}
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) {
window.setStatusBarColor(accountStatusBarColors[colorLevel]);
window.setStatusBarColor(color);
}
}
public void restore() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(defaultStatusBarColor);
}
public void updateWithDefaultColor() {
updateWithColor(accountPainter.getDefaultDarkColor());
}
}
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