Commit 30a3ab3a authored by Grigory Fedorov's avatar Grigory Fedorov

New ActionBarPainter extracted from ContactTitleActionBarInflater. Used in ContactAdd activity.

parent f275952b
......@@ -21,28 +21,15 @@ import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.xabber.android.data.Application;
import com.xabber.android.data.NetworkException;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.data.message.MessageManager;
import com.xabber.android.data.roster.PresenceManager;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.adapter.AccountChooseAdapter;
import com.xabber.android.ui.dialog.ContactDeleteDialogFragment;
import com.xabber.android.ui.helper.ActionBarPainter;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.androiddev.R;
import java.util.Collection;
import java.util.HashSet;
public class ContactAdd extends ManagedActivity implements ContactAddFragment.Listener {
public class ContactAdd extends ManagedActivity {
ActionBarPainter actionBarPainter;
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -54,6 +41,8 @@ public class ContactAdd extends ManagedActivity {
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp);
getSupportActionBar().setTitle(null);
actionBarPainter = new ActionBarPainter(this);
Intent intent = getIntent();
if (savedInstanceState == null) {
......@@ -109,4 +98,8 @@ public class ContactAdd extends ManagedActivity {
return EntityIntentBuilder.getUser(intent);
}
@Override
public void onAccountSelected(String account) {
actionBarPainter.update(account);
}
}
package com.xabber.android.ui;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
......@@ -32,6 +33,8 @@ public class ContactAddFragment extends GroupEditorFragment implements AdapterVi
private EditText nameView;
private View selectGroupsView;
Listener listenerActivity;
public static ContactAddFragment newInstance(String account, String user) {
ContactAddFragment fragment = new ContactAddFragment();
Bundle args = new Bundle();
......@@ -41,6 +44,12 @@ public class ContactAddFragment extends GroupEditorFragment implements AdapterVi
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
listenerActivity = (Listener)activity;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
......@@ -110,6 +119,12 @@ public class ContactAddFragment extends GroupEditorFragment implements AdapterVi
}
@Override
public void onDetach() {
super.onDetach();
listenerActivity = null;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedAccount = (String) accountView.getSelectedItem();
......@@ -118,6 +133,8 @@ public class ContactAddFragment extends GroupEditorFragment implements AdapterVi
onNothingSelected(parent);
setAccount(selectedAccount);
} else {
listenerActivity.onAccountSelected(selectedAccount);
if (!selectedAccount.equals(getAccount())) {
setAccount(selectedAccount);
setAccountGroups();
......@@ -159,4 +176,8 @@ public class ContactAddFragment extends GroupEditorFragment implements AdapterVi
MessageManager.getInstance().openChat(account, user);
getActivity().finish();
}
public interface Listener {
public void onAccountSelected(String account);
}
}
......@@ -28,7 +28,6 @@ import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.helper.ContactTitleActionBarInflater;
import com.xabber.android.ui.helper.ContactTitleExpandableToolbarInflater;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.androiddev.R;
import com.xabber.xmpp.address.Jid;
......
package com.xabber.android.ui.helper;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.view.Window;
import android.view.WindowManager;
import com.xabber.android.data.account.AccountManager;
import com.xabber.androiddev.R;
public class ActionBarPainter {
private int[] accountActionBarColors;
private int[] accountStatusBarColors;
private Window window;
private int defaultStatusBarColor;
private ColorDrawable defaultActionBarBackground;
private final ActionBarActivity activity;
public ActionBarPainter(ActionBarActivity activity) {
this.activity = activity;
accountActionBarColors = activity.getResources().getIntArray(R.array.account_action_bar);
accountStatusBarColors = activity.getResources().getIntArray(R.array.account_status_bar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window = this.activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
defaultStatusBarColor = window.getStatusBarColor();
}
TypedArray a = activity.getTheme().obtainStyledAttributes(R.style.Theme, new int[] {R.attr.colorPrimary});
int attributeResourceId = a.getResourceId(0, 0);
defaultActionBarBackground = new ColorDrawable(activity.getResources().getColor(attributeResourceId));
}
public void update(String account) {
int colorLevel = AccountManager.getInstance().getColorLevel(account);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(accountStatusBarColors[colorLevel]);
}
activity.getSupportActionBar().setBackgroundDrawable(new ColorDrawable(accountActionBarColors[colorLevel]));
}
public void restore() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(defaultStatusBarColor);
}
activity.getSupportActionBar().setBackgroundDrawable(defaultActionBarBackground);
}
}
package com.xabber.android.ui.helper;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.androiddev.R;
......@@ -22,71 +16,44 @@ public class ContactTitleActionBarInflater {
private final ActionBarActivity activity;
private View actionBarView;
private int[] accountActionBarColors;
private int[] accountStatusBarColors;
private Window window;
private int defaultStatusBarColor;
private Animation shakeAnimation = null;
private ColorDrawable defaultActionBarBackground;
private ActionBarPainter actionBarPainter;
public ContactTitleActionBarInflater(ActionBarActivity activity) {
this.activity = activity;
}
public void setUpActionBarView() {
accountActionBarColors = activity.getResources().getIntArray(R.array.account_action_bar);
accountStatusBarColors = activity.getResources().getIntArray(R.array.account_status_bar);
actionBarPainter = new ActionBarPainter(activity);
ActionBar actionBar = activity.getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBarView = LayoutInflater.from(activity).inflate(R.layout.contact_title, null);
actionBar.setCustomView(actionBarView, new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window = this.activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
defaultStatusBarColor = window.getStatusBarColor();
}
TypedArray a = activity.getTheme().obtainStyledAttributes(R.style.Theme, new int[] {R.attr.colorPrimary});
int attributeResourceId = a.getResourceId(0, 0);
defaultActionBarBackground = new ColorDrawable(activity.getResources().getColor(attributeResourceId));
}
public void update(AbstractContact abstractContact) {
actionBarPainter.update(abstractContact.getAccount());
activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setDisplayShowTitleEnabled(false);
actionBarView.setVisibility(View.VISIBLE);
ContactTitleInflater.updateTitle(actionBarView, activity, abstractContact);
int colorLevel = AccountManager.getInstance().getColorLevel(abstractContact.getAccount());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(accountStatusBarColors[colorLevel]);
}
activity.getSupportActionBar().setBackgroundDrawable(new ColorDrawable(accountActionBarColors[colorLevel]));
}
public void restoreDefaultTitleView(String title) {
actionBarPainter.restore();
activity.getSupportActionBar().setDisplayShowCustomEnabled(false);
activity.getSupportActionBar().setDisplayShowTitleEnabled(true);
actionBarView.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(defaultStatusBarColor);
}
activity.getSupportActionBar().setBackgroundDrawable(defaultActionBarBackground);
activity.setTitle(title);
}
......
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