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