If you think the Android project hubblog listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright 2012 GitHub Inc.//fromwww.java2s.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/package com.github.mobile.Accounts;
import android.accounts.Account;
import android.accounts.AccountManager;
import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.OutOfScopeException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Custom scope that makes an authenticated GitHub account available by
* enforcing that the user is logged in before proceeding.
*/publicclass AccountScope extends ScopeBase {
privatestaticfinal Key<GitHubAccount> GITHUB_ACCOUNT_KEY = Key
.get(GitHubAccount.class);
/**
* Create new module
*
* @return module
*/publicstatic Module module() {
returnnew AbstractModule() {
publicvoid configure() {
AccountScope scope = new AccountScope();
bind(AccountScope.class).toInstance(scope);
bind(GITHUB_ACCOUNT_KEY).toProvider(
AccountScope.<GitHubAccount> seededKeyProvider()).in(
scope);
}
};
}
privatefinal ThreadLocal<GitHubAccount> currentAccount = new ThreadLocal<GitHubAccount>();
privatefinal Map<GitHubAccount, Map<Key<?>, Object>> repoScopeMaps = new ConcurrentHashMap<GitHubAccount, Map<Key<?>, Object>>();
/**
* Enters scope using a GitHubAccount derived from the supplied account
*
* @param account
* @param accountManager
*/publicvoid enterWith(final Account account,
final AccountManager accountManager) {
enterWith(new GitHubAccount(account, accountManager));
}
/**
* Enter scope with account
*
* @param account
*/publicvoid enterWith(final GitHubAccount account) {
if (currentAccount.get() != null)
thrownew IllegalStateException(
"A scoping block is already in progress");
currentAccount.set(account);
}
/**
* Exit scope
*/publicvoid exit() {
if (currentAccount.get() == null)
thrownew IllegalStateException("No scoping block in progress");
currentAccount.remove();
}
@Override
protected <T> Map<Key<?>, Object> getScopedObjectMap(final Key<T> key) {
GitHubAccount account = currentAccount.get();
if (account == null)
thrownew OutOfScopeException("Cannot access " + key
+ " outside of a scoping block");
Map<Key<?>, Object> scopeMap = repoScopeMaps.get(account);
if (scopeMap == null) {
scopeMap = new ConcurrentHashMap<Key<?>, Object>();
scopeMap.put(GITHUB_ACCOUNT_KEY, account);
repoScopeMaps.put(account, scopeMap);
}
return scopeMap;
}
}