| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.android.internal.globalactions; |
| 17 |
|
| 18 |
import android.content.Context; |
| 19 |
import android.util.Log; |
| 20 |
import android.view.LayoutInflater; |
| 21 |
import android.view.View; |
| 22 |
import android.view.ViewGroup; |
| 23 |
import android.widget.ImageView; |
| 24 |
import android.widget.TextView; |
| 25 |
import com.android.internal.R; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public abstract class ToggleAction implements Action { |
| 32 |
private static final String TAG = "ToggleAction"; |
| 33 |
|
| 34 |
public enum State { |
| 35 |
Off(false), |
| 36 |
TurningOn(true), |
| 37 |
TurningOff(true), |
| 38 |
On(false); |
| 39 |
|
| 40 |
private final boolean inTransition; |
| 41 |
|
| 42 |
State(boolean intermediate) { |
| 43 |
inTransition = intermediate; |
| 44 |
} |
| 45 |
|
| 46 |
public boolean inTransition() { |
| 47 |
return inTransition; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
protected State mState = State.Off; |
| 52 |
|
| 53 |
|
| 54 |
protected int mEnabledIconResId; |
| 55 |
protected int mDisabledIconResid; |
| 56 |
protected int mMessageResId; |
| 57 |
protected int mEnabledStatusMessageResId; |
| 58 |
protected int mDisabledStatusMessageResId; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public ToggleAction(int enabledIconResId, |
| 68 |
int disabledIconResid, |
| 69 |
int message, |
| 70 |
int enabledStatusMessageResId, |
| 71 |
int disabledStatusMessageResId) { |
| 72 |
mEnabledIconResId = enabledIconResId; |
| 73 |
mDisabledIconResid = disabledIconResid; |
| 74 |
mMessageResId = message; |
| 75 |
mEnabledStatusMessageResId = enabledStatusMessageResId; |
| 76 |
mDisabledStatusMessageResId = disabledStatusMessageResId; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
void willCreate() { |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public CharSequence getLabelForAccessibility(Context context) { |
| 86 |
return context.getString(mMessageResId); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public View create(Context context, View convertView, ViewGroup parent, |
| 91 |
LayoutInflater inflater) { |
| 92 |
willCreate(); |
| 93 |
|
| 94 |
View v = inflater.inflate(R.layout.global_actions_item, parent, false); |
| 95 |
|
| 96 |
ImageView icon = v.findViewById(R.id.icon); |
| 97 |
TextView messageView = v.findViewById(R.id.message); |
| 98 |
TextView statusView = v.findViewById(R.id.status); |
| 99 |
final boolean enabled = isEnabled(); |
| 100 |
|
| 101 |
if (messageView != null) { |
| 102 |
messageView.setText(mMessageResId); |
| 103 |
messageView.setEnabled(enabled); |
| 104 |
} |
| 105 |
|
| 106 |
boolean on = ((mState == State.On) || (mState == State.TurningOn)); |
| 107 |
if (icon != null) { |
| 108 |
icon.setImageDrawable(context.getDrawable( |
| 109 |
(on ? mEnabledIconResId : mDisabledIconResid))); |
| 110 |
icon.setEnabled(enabled); |
| 111 |
} |
| 112 |
|
| 113 |
if (statusView != null) { |
| 114 |
statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId); |
| 115 |
statusView.setVisibility(View.VISIBLE); |
| 116 |
statusView.setEnabled(enabled); |
| 117 |
} |
| 118 |
v.setEnabled(enabled); |
| 119 |
|
| 120 |
return v; |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public final void onPress() { |
| 125 |
if (mState.inTransition()) { |
| 126 |
Log.w(TAG, "shouldn't be able to toggle when in transition"); |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
final boolean nowOn = !(mState == State.On); |
| 131 |
onToggle(nowOn); |
| 132 |
changeStateFromPress(nowOn); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public boolean isEnabled() { |
| 137 |
return !mState.inTransition(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected void changeStateFromPress(boolean buttonOn) { |
| 147 |
mState = buttonOn ? State.On : State.Off; |
| 148 |
} |
| 149 |
|
| 150 |
public abstract void onToggle(boolean on); |
| 151 |
|
| 152 |
public void updateState(State state) { |
| 153 |
mState = state; |
| 154 |
} |
| 155 |
} |